The Messenger of Allah ﷺ said, "If one of you is in the mosque and senses a movement of air between his buttocks, he should not leave until he hears a sound or detects an odor."
حدثنا قتيبة حدثنا عبد العزيز بن محمد عن سهيل بن أبي صالح عن أبيه
أن رسول الله ﷺ قال إذا كان أحدكم في المسجد فوجد ريحا بين أليتيه فلا يخرج حتى يسمع صوتا أو يجد ريحا
Takhrij: Also narrated by Muslim, Book of Menstruation, chapter on the evidence that whoever is certain of being in a state of purity, etc, hadith 362, from Suhayl through this chain. On this subject there are also narrations from Abdullah bin Zayd [Bukhari, hadith 137, 177, and Muslim, hadith 361], Ali bin Talq [Abu Dawud, hadith 205, 1005], Ayesha [Abu Dawud, hadith 1114, and Ibn Majah, hadith 1222], Ibn Abbas [Bayhaqi, 2/254], Ibn Masud [Tabarani, with different wording, see Majma al-Zawaid, 1/97], and Abu Saeed [Ibn Majah, hadith 514, and Ahmad, 3/96].