The prominent Muslim scholar and Da`iyah Sheikh `Abdul-Khaleq Hasan Ash-Shareef, states the following:

“If for unavoidable circumstances, and apparent necessity, a person of the opposite sex is treated by a person of the opposite gender, then there is nothing wrong. Thus, the main criterion in this case is the necessity, and that the treating doctor should abide by the ethics of his profession which restrict his medical examination to the diseased area only.

Stressing the above concept, we would like to cite for you the following fatwa issued by Sheikh Ahmad Kutty, a senior lecturer and an Islamic scholar at the Islamic Institute of Toronto, Ontario, Canada, in which he states the following:

“Islam has stipulated very strict rules with regard to the permissible boundaries of interactions/physical contacts between males and females who are not related either through marriage or blood relations.
according to these rules, it is considered Haram for males and females who are not related in marriage or blood to be in private or come into direct physical contacts with one another. As everything that leads to Haram is Haram, they ought to avoid circumstances or avenues that might lead to such close contacts as far as possible.
The above rules are designed specifically to protect us against our own vulnerabilities and weaknesses for as Allah says in the Qur’an, “Verily man/woman has been created weak.” (An-Nisa’: 28) As one poet rightly remarked, “All illicit relations can be traced to a single unlawful gaze, just like all burning fires often proceed from a single spark.”
Having said this, however, I must add the following: Since Islam takes into account the realities of everyday life, and, therefore there may be situations where such contacts are necessary, and therefore unavoidable, the scholars of Shari`ah have allowed certain exceptions to the above rule: Thus they have considered such contacts permissible if undertaken strictly for the purpose of saving lives or administering essential treatments wherever applicable. This special ruling falls under the rule of necessity, which stipulates that in exceptional cases what has been otherwise considered as impermissible becomes permissible.
In order to qualify for permission under the rule of necessity, however, certain conditions apply: Firstly, the physician of the same gender must be unavailable; secondly, contacts should be kept to the absolute minimum limit as necessary.

Once we have taken all possible measures that are humanly possible, Allah will hopefully forgive us the rest. After all, He will judge us not merely by our acts, but even more so by our intentions and cherished thoughts and motives.”