Answer
In the Name of Allah, Most Gracious, Most Merciful.
All praise and thanks are due to Allah, and peace and blessings be upon His Messenger.
In this fatwa:
It is not considered khalwah if you happen to be in an elevator with a non-mahram since you have no choice.
In his response to your question, Sheikh Ahmad Kutty, a senior lecturer and Islamic scholar at the Islamic Institute of Toronto, Ontario, Canada, states:
It is not considered khalwah if you happen to be in an elevator with a non-mahram since you have no choice. And we are told in the Qur’an that we are not accountable for things we have no control over.
However, when faced with a situation like that, you owe it to yourself to turn your mind away from sinful thoughts and stares, and focus your mind on dhikr so that you can guard yourself against falling into sins.
This is why we are told to arm ourselves with the weapon of duaa and dhikr while venturing out of home. The Prophet (peace be upon him) taught us to supplicate as follows: “Bismillahi tawakkaltu ala Allah laa hawla wala quwwata illa billah. Allahumma innee a`udhu bika an adilla aw udal aw adhilla aw udhall aw ajhala aw ujhala alayya (In the name of Allah, I place my trust in Allah; there is no power to seek good or strength to resist evil except by the will of Allah. O Allah, I seek Your protection against going astray or being led astray, or slipping up or being caused to slip up, or acting foolishly or others acting foolishly towards me).
And the Prophet tells us that when a person makes the above supplications three times, the angles will say: you are guarded and protected and Satan keeps a distance from him or her.
Almighty Allah knows best.