Taken from: Fatawa Arkan al-Islam
Question:
If a person performs an obligatory prayer behind an Imam who is offering a voluntary prayer, what is the ruling?
Specifically, if someone joins the congregation of Taraweeh while the Imam is leading it, but the person intends to perform the obligatory Isha prayer, is this permissible?
Answer:
Al-ḥamdu lillāh, waṣ-ṣalātu wa-s-salāmu ʿalā Rasūlillāh, ammā baʿd!
It is permissible according to Shari’ah to perform an obligatory prayer behind an Imam who is leading a voluntary prayer, and there is no objection to this.
For example, if a person wishes to perform the obligatory Isha prayer and joins an Imam who is leading Taraweeh, he may perform his obligatory prayer behind that Imam.
Statement of Imam Ahmad ibn Hanbal (رحمه الله):
◈ Imam Ahmad (رحمه الله) explained that if a traveler joins an Imam who is performing a voluntary prayer from the very beginning, then he should conclude his prayer by saying the salam along with the Imam.
◈ If, for any reason, a portion of the prayer remains incomplete, it should be completed after the Imam concludes with salam.
The Hadith of Muʿādh ibn Jabal (رضي الله عنه):
For further clarification, the narration of Jabir ibn ʿAbdillah (رضي الله عنه) is cited as evidence:
“ان معاذ بن جبل كان يصلي مع النبي ﷺ ثم يرجع فيؤم قومه“
(Ṣaḥīḥ al-Bukhārī, Kitāb al-Adhān, Bāb idhā ṭawwala al-imām…, ḥadīth: 700. Also see: 701, 705, 711, 6106)
Meaning:
“Muʿādh ibn Jabal (رضي الله عنه) used to perform prayer with the Prophet ﷺ, then return and lead his people in prayer.”
Clarification of the Issue:
◈ The prayer Muʿādh ibn Jabal (رضي الله عنه) performed behind the Prophet ﷺ was obligatory for him.
◈ When he led his people in prayer afterwards, it was voluntary for him but obligatory for them.
◈ This Hadith makes it clear that it is permissible to perform an obligatory prayer behind an Imam who is performing a voluntary prayer.
ھذا ما عندي والله أعلم بالصواب