The Messenger of Allah ﷺ prayed one rakah with one of the two groups while the other group faced the enemy. Then they withdrew and took the position of the first group, and the first group came forward, and he prayed another rakah with them and gave the closing salaam. Then this group stood and completed their remaining rakah, and the other group stood and completed their remaining rakah.
حدثنا مسدد حدثنا يزيد بن زريع عن معمر عن الزهري عن سالم
أن رسول الله ﷺ صلى بإحدى الطائفتين ركعة والطائفة الأخرى مواجهة العدو ثم انصرفوا فقاموا في مقام أولئك وجاء أولئك فصلى بهم ركعة أخرى ثم سلم عليهم ثم قام هؤلاء فقضوا ركعتهم وقام هؤلاء فقضوا ركعتهم
Bukhari transmitted it in the Book of Military Expeditions, Chapter on the Expedition of Dhat al-Riqa, hadith no. 4133, from Musaddad, and Muslim transmitted it in the Book of the Travelers' Prayer, Chapter on the Prayer of Fear, hadith no. 839, from the hadith of Mamar, through this same chain.