There is no definitive scientific answer to why some people are transgender. Gender identity, anatomical sex, and sexual orientation are determined while an individual is in the womb. Although the anatomy happens in the first six weeks, gender identity starts actualizing at around six months gestation. It is at this point that the brain feminizes or masculinizes.
A testosterone surge will stimulate the fetal brain to develop in a male direction. Without the surge, however, the fetal brain will develop towards a female direction. With transgender individuals, the genitals will not match the brain direction. Therefore, these individuals will feel trapped in a different body.