It is well established in the Shari`ah that facing the qiblah (direction of the Prayer) is a prerequisite for the validity of salah. However, there are some situations in which the obligation to face the qiblah is relaxed.
Sheikh M. S. Al-Munajjid, a prominent Saudi Muslim lecturer and author, states: Perhaps the questioner wants to know about the situations in which the obligation to face the qiblah while praying is waived, and it is valid to face a direction other than the qiblah.

One of the conditions of Prayer being valid is facing the qiblah, and Prayer is not valid without that because Allah has enjoined that repeatedly in the Qur’an. Allah says: [So turn your face in the direction of Al-Masjid Al-Haram (at Makkah). And wheresoever you people are, turn your faces (in Prayer) in that direction] (Al-Baqarah 2:144).
However, there are three exceptions to the above ruling:
1. If a person is unable to do that, such as someone who is sick and is facing a direction other than the qiblah and cannot turn his face towards it. The duty to face the qiblah is waived in this case, because Allah says: [So keep your duty to Allah and fear Him as much as you can] (At-Taghabun 64:16) and [Allah burdens not a person beyond his scope] (Al-Baqarah 2:286).
Also, the Prophet (peace and blessings be upon him) said, “If I command you to do a thing, then do as much of it as you can” (Al-Bukhari, 7288 and Muslim, 1337).
2. If a person is in a state of intense fear, such as someone who is fleeing from an enemy or from a wild animal, or from a flood that may drown him. In that case he should pray no matter what direction he is facing. The evidence for that is the Qur’anic verse that reads: [And if you fear (an enemy), perform salah (Prayer) on foot or riding. And when you are in safety, offer the salah in the manner He has taught you, which you knew not (before)] (Al-Baqarah 2:239).
The phrase “if you fear” is general in meaning and includes all kinds of fear. And the phrase “And when you are in safety, offer the salah (Prayer) in the manner He has taught you, which you knew not (before)” indicates that whatever part of the Prayer a person omitted because of fear, there is no sin on him because of that, and that includes facing the qiblah.
This is also supported by the two verses quoted above, and the hadith in which it says that obligations are connected to one’s ability to do them.
3. When offering nafl (supererogatory) Prayers when traveling, whether by plane, car, or an animal, one may pray in whatever direction one is facing. This applies to nafl Prayers such as Witr, Night Prayers (Qiyam), Duha, and so on.
The traveller should offer all the nafl Prayers exactly like one who is not traveling, except the regular sunnah Prayers such as the sunnahs of Zhuhr, Maghrib and `Isha’. The sunnah is not to offer these prayers when traveling.
If a traveller wants to offer nafl Prayers, he may offer them whatever direction he is facing. This is narrated in As-Saheehayn from the Messenger of Allah (peace and blessings be upon him).
In these three cases it is not obligatory to face the qiblah. With regard to someone who is ignorant about the direction of the qiblah, he has to face it; but if he tries to work it out then he finds out that he got it wrong, he does not have to repeat the Prayer. We do not say that the obligation to face the qiblah is waived; rather he has to face the qiblah and he should try his best to do so. If he does his best then finds out that he got it wrong, he does not have to repeat his Prayer. The evidence for that is the fact that the Companions who were unaware that the qiblah had been changed to the Ka`bah were praying Fajr one day in the mosque of Quba’ when a man came to them and said, “(Verses from the) Qur’an have been revealed to the Messenger of Allah (peace and blessings be upon him) this night, and he has been commanded to face the Ka`bah, so turn your faces to it.” They were facing towards Syria and they turned their faces towards the Ka`bah (Al-Bukhari 304 and Muslim 526). The Ka`bah had been behind them; they made it in front of them, so they turned around but continued to pray. This happened at the time of the Messenger of Allah (peace and blessings be upon him), but he did not denounce it.
Accordingly, if a person makes a mistake in the direction of the qiblah because he is unaware of it, he does not have to repeat the Prayer; but if he comes to know of it, even during the Prayer, then he has to turn his face to the qiblah.
In brief, facing the qiblah is one of the conditions of Prayer without which the Prayer is not valid, except in the three cases mentioned above, unless a person makes a mistake after doing his best to find out.