Sayyiduna Abu Bakrah (may Allah be pleased with him) narrates: The Messenger of Allah (peace and blessings be upon him) led us in the prayer of fear. He (peace be upon him) led some people in two rak‘ahs of prayer and said the salam. These people left, and others came and stood in their place. Then he (peace be upon him) led them in two rak‘ahs of prayer and said the salam. Thus, the Prophet (peace be upon him) performed four rak‘ahs, and the people performed two each.
Hadith Referenceالفتح الربانی / أبواب صلاة الخوف وهى أنواع / 2963
Benefits: … In the four units (rak‘ahs) of prayer of the Prophet sallallahu alayhi wa sallam, the obligatory prayer was only two units (rak‘ahs). From this hadith, it is established that when the imam is performing a voluntary (nafl) prayer, it is permissible to follow him in obligatory (fard) prayer, because when the Messenger of Allah sallallahu alayhi wa sallam was leading the second group, it was his voluntary (nafl) prayer.