Sami Saleem, 30, had filed a petition with the Rawalpindi-based bench of the Lahore High Court in which he had said that his physical characteristics were like those of a man while his feelings and thoughts were like those of a woman.
Saleem said he had consulted a psychiatrist and had undergone counselling for two years, and that doctors had confirmed that he was suffering from gender identity disorder and needed an operation to change his sex.
He said he had contacted many doctors to conduct the gender reassignment surgery but all of them had declined, saying that there is no law in Pakistan which permitted them to carry out such an operation.
The doctors also advised him to get permission from court, he said.
Acting on Saleem's plea, a single-judge bench in Rawalpindi allowed him to undergo an operation to change his sex.
Earlier, Lahore High Court Chief Justice Khwaja Muhammad Sharif had dismissed a similar petition filed by Saleem, saying he should approach the Rawalpindi bench as he lived in the garrison city.
Saleem had mentioned in his petition that gender reassignment surgeries were being conducted in several Islamic countries, including Iran and Egypt.
However, observers said today's verdict is expected to trigger a heated debate, especially among religious groups and Islamic clerics who are opposed to such practices.