The 1988 fatwa by Egyptian Grand Mufti of the republic, Mohammad Sayed Tantawi, on sex reassignment states the following:
To the honored general secretary of the Doctors’ General Syndicate. This is an answer to the Syndicate’s letter number 483 of May 14, 1988, asking for the opinion of religion on the matter of a student of medicine at the al-Azhar university, who has been subjected to a surgical operation (removing his male organs) in order to turn him into a girl.
We find that Usama ibn Sharik tells: “A bedouin came to the Prophet and said, ‘O, Messenger of God, can you cure?’ And He said, ‘Yes, for God did not send a disease without sending a cure for it, knowing it from His knowledge….’” This [hadith] is told by Ahmad [ibn Hanbal]. There is another version: “Some bedouins said, ‘O, Messenger of God, can you cure?’ And He said. ‘Yes. God’s servants can cure themselves, for God never gave a disease without providing a cure or a medicine for it, except for one disease.’ They asked, ‘O, Prophet of God, what disease is that?’ He said, ‘old age.’” This version is related by ibn Maja abu Da’ud, at-Tirmidhi, and others. (Muntaqi l-Akhbar wa Sharhan nayl al-Awtar, v. 8, p. 200, and Fath al-Bari bi Sharh Sahih al-Bukhari, by al-cAsqalani(29), v. 9, p. 273, in the chapter on those who imitate women).
As for the condemnation of those who by word and deed resemble women, it must be confined to one who does it deliberately [tacahhada dhalika], while one who is like this out of a natural disposition must be ordered to abandon it, even if this can only be achieved step by step. Should he then not comply, but persist [in his manners], the blame shall include him, as well … especially if he displays any pleasure in doing so.
The person who is by nature a hermaphrodite [mukhannath khalqi] is not to be blamed. This is based on [the consideration that] if he is not capable of abandoning the female, swinging his hips in walking and speaking in a feminine way, after having been subjected to treatment against it, [he is at least willing to accept that] it is still possible for him to abandon it, if only gradually. But if he gives up the cure with no good excuse, then he deserves blame.
At-Tabari took it as an example that the Prophet (God bless him and grant him salvation) did not forbid the hermaphrodite from entering the women’s quarters until he heard him giving a description of the woman in great detail. Then he prohibited it. This proves that no blame is on the hermaphrodite for simply being created that way.
That being so, the rulings derived from these and other noble hadiths on treatment grant permission to perform an operation changing a man into a woman, or vice versa, as long as a reliable doctor concludes that there are innate causes in the body itself, indicating a buried [matmura] female nature, or a covered [maghmura] male nature, because the operation will disclose these buried or covered organs, thereby curing a corporal disease which cannot be removed, except by this operation.
This is also dealt with in a hadith about cutting a vein, which is related through Jabir: “The Messenger of God sent a physician to abu ibn Kacb. The physician cut a vein and burned it.” This hadith is related by Ahmad [ibn Hanbal] and Muslim. What supports this view is what al-Qastallani (30) and al-cAsqalani say in their commentaries on it: “This means that it is incumbent upon the hermaphrodite to remove the symptoms of femininity.”
And this is further sustained by the author of Fath al-Bari who says “…having given him treatment in order to abandon it…” This is a clear proof that the duty prescribed for the hermaphrodite can take the form of a treatment. The operation is such a treatment, perhaps even the best treatment. This operation cannot be granted at the mere wish to change sex with no clear and convincing corporal motives. In that case it would fall under that noble Hadith which al-Bukhari relates through Anas: “The Messenger of God cursed the hermaphrodites among the men and the over-masculine women, saying ‘expel them from their houses’, whereupon the Prophet himself (God bless Him and grant Him salvation) expelled one, and Umar expelled another one.” This Hadith is related by Ahmad and al-Bukhari.
To sum up: It is permissible to perform the operation in order to reveal what was hidden of male or female organs. Indeed, it is obligatory to do so on the grounds that it must be considered a treatment, when a trustworthy doctor advises it. It is, however, not permissible to do it at the mere wish to change sex from woman to man, or vice versa.
Praise be to He who created, who is mighty and guiding. From what has been said the answer to what was in the question will be known. Praise be to God the most High.
 Quoted in Jakob Skovgaard-Petersen, "Sex Change in Cairo: Gender and Islamic Law,"Journal of the International Institute(Spring 1995).