Rather you should join the imam in the prayer, with the intention of praying Maghrib, then you should sit in the third rak’ah and recite the Tashahhud and say the salaam, then join the imam in what is left of ‘Isha’ prayer. Or wait during the Tashahhud until the imam finishes the prayer and say the tasleem with him, then pray ‘Isha’.
This is the view of Imam al-Shaafa’i (may Allaah have mercy on him), and is one of the two views narrated from Imam Ahmad. Al-Mardaawi stated in al-Insaaf (4/413) that this view was favoured by a number of the companions of Imam Ahmad, including Shaykh al-Islam Ibn Taymiyah and his grandfather al-Majd ibn Taymiyah.
Al-Nawawi (may Allaah have mercy on him) said in al-Majmoo’, 4/143:
If he intends to pray Fajr behind one who is praying Zuhr and the congregational prayer is completed, if he wishes he may wait during the Tashahhud until the imam finishes, and say the tasleem with him, which is better, or if he wishes he may decide to leave him and say the tasleem. This leaving does not invalidate his prayer, and there is no scholarly dispute on this point, because he is excused from following the imam. End quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) was asked:
Some worshippers were delayed in praying Maghrib, and they found that the imam had started to pray ‘Isha’. Should they pray Maghrib in a congregation on their own, or join the imam? And how should they pray?
The correct view is that if a person comes (to the mosque) and the imam is praying ‘Isha’, whether he is with a group or not, he should join the imam, with the intention of praying Maghrib. It does not matter if the imam’s intention is different from that of one who is praying behind him, because the Prophet (peace and blessings of Allaah be upon him) said: “Actions are but by intentions, and each person will have but that which he intended.” If they join him in the second rak’ah, they can say the tasleem with him, because they will have prayed three rak’ahs, and it does not matter if they sit for the Tashahhud during their first rak’ah. If they join him in the first rak’ah, then when he stands up for the fourth rak’ah, they can sit and recite the Tashahhud and say the tasleem, then join him in what remains of ‘Isha prayer.
The second opinion concerning this matter is that they should join him with the intention of praying ‘Isha’, then pray Maghrib after that, and the obligation to offer the prayers in order is waived because of the duty to join the congregation.
The third view is that they should pray Maghrib on their own, then join him in whatever is left of ‘Isha’ prayer. The last two views involve something about which there is some reservation. The first view involves neglecting the order of prayers by putting ‘Isha’ before Maghrib. The second view involves having two congregations in one mosque at the same time, which is dividing the ummah.
But the first view which we mentioned is the correct one. Someone may say that there is something wrong, which is saying the tasleem before the imam. In fact there is nothing wrong with this. It is narrated in some places in the Sunnah that a member of the congregation may do something separately from the imam, such as in the fear prayer, where the imam leads them in one rak’ah, then they complete the prayer by themselves and leave.
Another example is the story of the man who joined Mu’aadh ibn Jabal (may Allaah be pleased with him) in prayer, but when he started to recite Soorat al-Baqarah or another similar soorah, he left him and did not complete the prayer with him.
And the scholars said that if a man is praying in congregation then he feels that is about to pass wind or that he needs to break his wudoo’ by urinating or defecating, then there is no sin on him if he decides to pray alone and completes his prayer and leaves. This indicates that praying alone in the case of need is not regarded as wrong. End quote.
Liqaa’aat al-Baab il-Maftooh, 3/425
Shaykh Ibn Baaz was asked: I entered the mosque and the ‘Isha’ prayer had started. Before I joined the prayer, I remember that I had not prayed Maghrib. Should I pray Maghrib and then catch up with ‘Isha’ with the congregation, or should I pray with the congregation and then pray Maghrib afterwards?
He replied: if you enter the mosque and ‘Isha’ prayer has started, then you remember that you did not pray Maghrib, then you should join the congregation with the intention of praying Maghrib. When the imam stands up for the fourth rak’ah, you should sit in the third rak’ah and recite the final tashahhud – i.e., al-tahhiyaatu and the blessings upon the Prophet (peace and blessings of Allaah be upon him) – and make du’aa’ after that, then wait for the imam to say the tasleem and say the tasleem with him. It does not matter if the imam and the member of the congregation have different intentions, according to the sound scholarly opinion. If you pray Maghrib on your own and then join the congregation in what is left of ‘Isha’ prayer, there is nothing wrong with that. End quote.
Majmoo’ Fataawa Ibn Baaz, 12/189
The Standing Committee was asked: what should a man do if he forgets Fajr prayer, for example, and does not remember until the iqaamah is given for Zuhr, or he forgets Zuhr and does not remember until the time for ‘Asr begins. Should he join the imam with the intention of offering the obligatory prayer that he missed or with the intention of offering the prayer that is currently due, and make up the missed prayer after that?
He replied: He should offer the prayer that he forgot behind the imam, and it does not matter if his intention is different from that of the imam, according to the sound scholarly opinion. End quote.
Fataawa al-Lajnah al-Daa’imah, 7/407.