Praise be to Allah.
The Prophet (blessings and peace of Allah be upon him) forbade praying when resisting the urge to relieve oneself and when one is distracted by trying to keep it from coming out.
It was narrated that ‘Aa’ishah (may Allah be pleased with her) said: I heard the Messenger of Allah (blessings and peace of Allah be upon him) say: “There is no prayer when food is ready, or when one is resisting the urge to relieve oneself (by urinating or defecating).”
Narrated by Muslim (560).
Imam an-Nawawi (may Allah have mercy on him) said in his commentary on Saheeh Muslim (5/46):
In these hadeeths we see that it is makrooh to pray when food is ready that one wants to eat, because this is a distraction and it causes one to not have proper focus. It is also makrooh when one is resisting the urge to relieve oneself – which refers to urine and stools. End quote.
But this regarding it as being makrooh applies only to starting to pray whilst resisting the urge to relieve oneself. But if a person starts to pray when he does not feel that he is resisting the urge to defecate or urinate, and he only starts to feel that in the middle of the prayer, then in that case it is not makrooh if it does not prevent him from completing his prayer.
The Standing Committee for Academic Research and Issuing Fatwas was asked: Sometimes I resist the urge to defecate before praying, and I pray but I do not feel that urge during the prayer; is my prayer acceptable? Sometimes the opposite happens; is my prayer acceptable?
The committee replied:
It is not permissible for the worshipper to start praying when he is resisting the urge to defecate or urinate, because the Prophet (blessings and peace of Allah be upon him) said: “There is no prayer when food is ready, or when one is resisting the urge to relieve oneself.” Narrated by Muslim in his Saheeh.
The reason behind that – and Allah knows best – is that this prevents proper focus in the prayer, but if he prays in that state, his prayer is still valid, but it is lacking and is imperfect, because of the hadeeth quoted above, and he does not have to repeat it. But if you start to pray when you are not resisting the urge to relieve yourself, and you only start to feel that during the prayer, then the prayer is valid and is not makrooh, provided that this urge did not prevent you from completing the prayer.
End quote from Fataawa al-Lajnah ad-Daa’imah li’l-Buhooth al-‘Ilmiyyah wa’l-Ifta’, 7/25-26
See also the answer to question no. 159503
If a person is not sure whether wind came out of him, his wudoo’ is not invalidated by this mere uncertainty; rather he has to carry on with his prayer, and in this case his prayer is valid and he does not have to repeat it, unless he is certain that he did pass wind.
Al-Bukhaari (137) and Muslim (361) narrated from Sa‘eed ibn al-Musayyab, and from ‘Abbaad ibn Tameem, from his paternal uncle, that he complained to the Messenger of Allah (blessings and peace of Allah be upon him) about when a man thinks that something has happened during the prayer. He said: “Do not stop unless you hear a sound or notice a smell.”
It was narrated that Abu Hurayrah said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “If one of you feels something in his stomach and is not sure whether something came out of him or not, let him not leave the mosque unless he hears a sound or notices a smell.”
Narrated by Muslim (362).
For more information, please see fatwa no. 114793
But it should be noted that what is meant by hearing a sound or noticing a smell refers to when one is certain that he has passed wind and broken his wudoo’, no matter what it is that has broken it.
Al-Haafiz Ibn Hajar said: The hadeeth indicates that prayer is valid so long as one is not certain of having broken wind; it does not mean that the only way to be certain is by these two things – i.e., smelling and hearing.
End quote from Fath al-Baari Sharh Saheeh al-Bukhaari (1/238)
In Fataawa al-Lajnah ad-Daa’imah li’l-Buhooth al-‘Ilmiyyah wa’l-Ifta’ (5/255) it says:
Question: does all wind that comes out of a man invalidate his wudoo’ (I know that it does invalidate it), but my question is: when does it invalidate it, i.e., does it invalidate it when there is a sound and a smell and a sensation all together, or is it when there is a sound and a smell only, and the sensation is not included in that?
I hope that you will explain this matter to me, may Allah bless you, because I am confused about this matter.
Answer: praise be to Allah alone, and blessings and peace be upon His Messenger and his family and companions. To proceed:
What you have mentioned about things that invalidate wudoo’ apply when one is certain that something has come out of him because he heard a sound or noticed a smell, or because of any other thing by which one becomes certain that he has passed wind. That is because the Prophet (blessings and peace of Allah be upon him) said, when he was asked about a man who feels something during the prayer: “He should not stop until he hears a sound or notices a smell.” Saheeh – agreed upon. End quote.
This principle – that certainty cannot be dispelled by uncertainty – is a general principle and does not apply only to purification and prayer.
Imam an-Nawawi (may Allah have mercy on him) said:
This hadeeth points to one of the basic principles of Islam and one of the most important guidelines of fiqh, which is that things are deemed to remain as they originally were unless one becomes certain that they are otherwise, and fleeting uncertainty does not affect that. An example of that is the issue mentioned in the hadeeth, which is that if a person is certain that he was in a state of purity and is uncertain as to whether that has been invalidated, then he is deemed to still be in a state of purity, and it makes no difference whether this uncertainty arose during the prayer itself or outside of prayer. This is our view and the view of the majority of scholars of the earlier and later generations.
Another example of the principle mentioned above is when a man is uncertain as to whether he has divorced his wife or freed his slave, or he is uncertain as to whether pure water has been rendered impure, or something impure has been purified, or whether a garment or food or anything else is impure, or whether he has prayed three rak‘ahs or four, or he has bowed and prostrated or not, or whether he formed the intention to fast or pray or do wudoo’ or observe i‘tikaaf whilst he is doing these acts of worship, and other similar matters. All of this uncertainty has no impact at all, and the basic principle remains that this thing has not happened.
The scholars made exceptions from this principle in some cases, which are well known in the books of fiqh and there is no room to explain them here. They are discussed in many books, there are counter-arguments and responses thereto, and there are differences of opinion concerning some of them.
End quote from Sharh Saheeh Muslim by an-Nawawi (4/49-50)
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
This – I mean basing things on certainty and ignoring uncertainty – is an important principle which is supported by the words of the Prophet (blessings and peace of Allah be upon him): “If one of you is uncertain about his prayer, let him ignore what is uncertain and continue on the basis of what is certain.” There are many issues connected to this principle in matters of divorce, contracts and so on; if a person were to follow them it would solve many problems and dispel a great deal of waswaas (compulsive thoughts) and doubts. This is by the blessing of the words and rulings of the Prophet (blessings and peace of Allah be upon him).
It is also one of the ways in which Islam makes things easy, for the aim is not to subject the Muslims to anxiety and confusion; rather the aim is to make their affairs clear and straightforward. If a person were to give in to such uncertainty, his life would be hard, because the Shaytaan will never stop at these whispers and doubts about matters of purification only; rather he will try to affect him in matters of prayer, fasting and other things, and in all areas of his life, even with his family. So the Lawgiver seeks to cut off these whispers at their roots by telling us to ignore them and to ward them off so that they will not have any effect on us.
End quote from ash-Sharh al-Mumti‘ ‘ala Zaad al-Mustaqni‘, 1/312
And Allah knows best.