Answer: All perfect praise be to Allah the Lord of The Worlds. May His blessings and peace be upon our Prophet Mohammad and upon all his family and companions.
Islamic Sharia permits and encourages seeking treatment. According to the view of the Muslim jurists, studying medicine and specializing in its different sciences is a collective duty. In principle, a female patient must be treated by a female doctor while a male patient must be treated by a male doctor. Adequate measure must be taken to provide male as well as female doctors in various medical specializations and ensuring this is a collective duty as well. However, if a doctor of the same sex of patient is hard to find and the patient is need for treatment, then it is permissible for a male doctor to treat a female patient and vice versa.
It is stated in the Shafie book Hashiyat al-Bajirmi, V.3:P.380 : "It is permissible that a female doctor treats a male patient in case a male doctor wasn`t available."
From an Islamic perspective, there are conditions and rules regarding male doctor treating female patient and vice versa:
First: It isn`t permissible to look at the Awrah (private parts) save as needed. Islamic Sharia has specified the Awrah of both man and woman, so it isn`t permissible for a man to look at or touch the Awrah of a non-Mahram woman, because such an act is indecent and constitutes a breach of the moral code. This is attested to in the narration where the Messenger of Allah said, "A man must not look at a man's private parts nor must a woman look at a woman's private parts; neither should two men lie naked under one cover, nor should two women lie naked under the same cover." [Reported by Muslim]. In principle, it is forbidden to look at someone`s Awrah or touch it unless there is a necessity and provided that this is done as needed because the Sharia maxim, in this regard, states: "Necessity must only be assessed and answered proportionately."
Second: When there is a necessity that a male doctor treat a female patient, then it must be made sure that there is no Khilwa involved. Here, Khilwa refers to a man having privacy with a woman in a suspicious manner. For example, when a man meets with a woman in an unfrequented place or a closed room. However, there is no Khilwa in case the female patient is examined or taken to the operation room in the presence of her husband, one of her Mahrams or a female nurse. It is stated in the book [Bushra Al-Kareem, V.1:P.602]: "It is permissible that a man be in Khilwa with two women, but it is forbidden that two men be in Khilwa with one woman."
Nowadays, there are plenty of specialized female doctors working in different hospitals and medical centers. In principle, a female patient must seek to be treated by a female doctor. If this was difficult, then a female patient may go to a male doctor provided that the aforementioned conditions and rules are adhered to. And Allah the Almighty knows best.