Praise be to Allah.
Turning away in prayer is of different types:
1. Turning away physically, by turning one’s chest away from the direction of the qiblah. This type of turning away invalidates the prayer, because facing towards the qiblah is one of the conditions of prayer being valid.
For more information, please see the answer to question no. 65853 .
2. Turning away one’s head or eyes only, whilst the body remains facing towards the qiblah. This type of turning away is disliked (makrooh), unless the Muslim does it because he needs to.
If he does it with no need, then he has undermined the reward for his prayer but it is still valid and is not rendered invalid by that.
It says in al-Mawsoo‘ah al-Fiqhiyyah (27/109):
There is no difference of opinion among the fuqaha’ regarding the fact that it is disliked (makrooh) to turn away in prayer, because of the hadith of ‘Aa’ishah (may Allah be pleased with her), who said: I asked the Prophet (blessings and peace of Allah be upon him) about turning away in prayer and he said: “It is something that the Shaytaan steals from a person’s prayer.” Narrated by al-Bukhaari (751).
It is only disliked (makrooh) if it is done unnecessarily or without an excuse. But if there is a need for that, such as if the worshipper fears for himself or his property, then it is not disliked. End quote.
It says in Fataawa al-Lajnah ad-Daa’imah (7/27):
Turning away in prayer is disliked and undermines its reward, but the one who turns away in prayer is not obliged to repeat his prayer, because in other hadiths there is evidence which indicates that it is permissible to turn away if there is a need to do so. Thus it is known that it does not invalidate the prayer. End quote.
There are many hadiths which indicate that it is permissible to turn away in prayer if that is done for a reason, such as the report narrated by Muslim (431) from Jaabir (may Allah be pleased with him), who said: The Messenger of Allah (blessings and peace of Allah be upon him) was sick, so we prayed behind him when he was sitting, and Abu Bakr made the people hear the takbeer. He turned to look at us and saw that we were standing, so he gestured to us that we should sit down, and we prayed behind him sitting.
Abu Dawood (916) narrated that Sahl ibn al-Hanzaliyyah (may Allah be pleased with him) said: The iqaamah for prayer – meaning Fajr prayer – was given, and the Messenger of Allah (blessings and peace of Allah be upon him) began to pray whilst he kept turning and glancing up at the mountain pass.” Abu Dawood said: He had sent a rider to the mountain pass to stand guard overnight. Classed as saheeh by all-Albaani in Saheeh Abi Dawood.
Shaykh Ibn Baaz (may Allah have mercy on him) said: There is nothing wrong with turning away whilst praying to seek refuge with Allah from the accursed Shaytaan when experiencing intrusive thoughts (waswasah); rather it is recommended (mustahabb) when there is an urgent need for it, but it should be done with the head only." (Majmoo‘ Fataawa Ibn Baaz 11/130).
3. There is another type of turning away, which is turning away whilst praying in one’s heart, through thoughts that distract a person so that he does not focus and reflect on his prayer.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
You should understand that turning away is of two types:
1. Turning away in a physical sense, which is turning the head;
2. Turning away in a metaphorical sense, in the heart. This refers to waswaas and passing thought that come to the heart. This is a problem that no one is free of; how difficult it is to treat it and how few are those who are safe from it. It undermines the prayer, and would that it was only in part of the prayer. But a person may turn away from the beginning of his prayer until the end, and it may truly be said that this is somethings that the Shaytaan steals from a person’s prayer." (Ash-Sharh al-Mumti‘ 3/70).
And Allah knows best.