The Ruling on Praying in the Nearby Mosque
Prophetic Hadith:
It is narrated from Abdullah ibn Umar (may Allah be pleased with him) that the Messenger of Allah ﷺ said:
“A man should pray in the mosque that is near him. He should not go around searching for other mosques.”
Juristic Ruling:
If the imam of the nearby mosque holds beliefs based on disbelief (kufr) and polytheism (shirk), then it is not permissible to pray behind such an imam.
If the imam is involved in innovations or in transgression and wrongdoing, then it is permissible to go to another, more distant mosque and pray there.
Not Completing the Prayer Behind the Imam Because of an Excuse
Prophetic Hadith:
It is narrated from Jabir ibn Abdullah (may Allah be pleased with him) that:
A man came with two camels used for carrying water. Night had fallen. He found Mu’adh leading the Isha prayer, so he made the camels sit down and joined the prayer. Mu’adh (may Allah be pleased with him) began Surah al-Baqarah, so that man ended his prayer with the salam, completed his prayer separately and left. Then he complained to the Messenger of Allah ﷺ:
“O Messenger of Allah! We are people who work hard. Mu’adh first prays Isha with you, then comes to us late and begins a long recitation.”
The Prophet ﷺ said to Mu’adh:
“O Mu’adh! Are you putting people to trial? Are you making people averse and causing discord?”
Conclusion:
From this hadith it is learned that an obligatory prayer may be performed behind an imam who is praying a voluntary prayer, just as Mu’adh (may Allah be pleased with him) would perform the obligatory prayer with the Messenger of Allah ﷺ and then lead his people in prayer.
Fatwa:
His Eminence Shaykh Mufti Abd al-Aziz ibn Baz (may Allah have mercy on him) says:
“According to the correct view of the scholars, there is no harm in praying the Isha prayer with that intention behind an imam who is leading the Tarawih prayer. When the imam ends the prayer with the salam, the follower should complete the rest of his prayer.”
Performing the Prayer Twice
Prophetic Hadith:
It is narrated from Mihjan (may Allah be pleased with him):
He was present with the Messenger of Allah ﷺ in a gathering. The call to prayer was made, so the Prophet ﷺ led the prayer, then returned. Mihjan was sitting there and did not join the prayer. The Prophet ﷺ said:
“What prevented you from praying with the people? Are you not a Muslim?”
Mihjan (may Allah be pleased with him) said:
“Of course I am, but I have already prayed in my house.”
The Prophet ﷺ said:
“When you come, pray with the people even if you have already prayed.”
The Difference Between the Intention of the Imam and the Follower
Prophetic Hadith:
Mu’adh (may Allah be pleased with him) would pray with the Messenger of Allah ﷺ, then come to his people and lead them in prayer.
Juristic Ruling:
In this case the prayer was voluntary for Mu’adh (may Allah be pleased with him), while for those praying behind him the same prayer was obligatory.
From this it is learned that the intention of the imam and of the follower in prayer may differ.