Answer:
There are three possible options:
➊ He should pray Maghrib in a corner of the mosque, and after that join the Isha prayer. Whatever portion he catches, he should pray.
➋ He should first pray Isha behind the imam. After Isha, he should pray Maghrib. If the order is disturbed because of an excuse and necessity, there is no harm.
﴿رَبَّنَا لَا تُؤَاخِذْنَا إِنْ نَسِينَا أَوْ أَخْطَأْنَا﴾
“Our Lord! Do not hold us accountable if we forget or make a mistake.”
➌ He should wait until one rak’ah of Isha has passed, then stand behind the imam with the intention of Maghrib, and give the salam together with the imam. A difference between the intention of the imam and that of the follower does no harm.