Please have a read of the below article:
Praise be to Allaah.
If you join the congregation during the first tashahhud of Maghrib prayer, then you should follow the imam in the third rak’ah, and recite the tashahhud, then get up after he says the salaam to complete your prayer. You have two rak’ahs left, so pray the first one with al-Faatihah and another soorah, then recite the tashahhud, which is the first tashahhud for you, then get up for the third rak’ah, and recite al-Faatihah only, and recite the last tashahhud, then say the salaam.
What is stated above is based on the assumption that what the latecomer caught up with with the imam is the beginning of his own prayer, and what he prays on his own is the end of his prayer. This is the opinion of al-Shaafa’i (may Allaah have mercy on him).
Al-Nawawi (may Allaah have mercy on him) said in al-Majmoo’ (4/117): If he catches up with a rak’ah of Maghrib he should stand up after the imam says the salaam and pray one rak’ah and recite the tashahhud, then pray a third rak’ah and recite the tashahhud.
Please visit https://islamqa.info/en/49037
also visit https://islamqa.info/en/127293