Name: Tahiyyatul Masjid, prayer for greeting the mosque, greeting of the mosque
Ruling: The majority opinion is that it is mustahabb, Sunnah Mu’akkadah, or recommended sunnah. However, some scholars hold that it is obligatory.
Timing: Whenever one enters a mosque, even in the disallowed prayer times, as it is done for a specific reason.
Abu Qataadah (may Allah be pleased with him) said: The Prophet (blessings and peace of Allah be upon him) said: “When one of you enters the mosque, let him not sit down until he has prayed two rak‘ahs.” (Bukhari and Muslim)
Number of Units: 2
These two units can be prayed separately from any other prayer. Or if one enters a mosque and prays an appointed prayer, it would count as his tahiyyatul masjid.
For example, the Standing Committee said:
“Praying the two rak’ahs of Fajr will count for itself and for Tahiyyat al-Masjid. This is better, but if he prays Tahiyyat al-Masjid first, then two rak’ahs of Fajr, that is OK.” (Taken from https://islamqa.info/en/21206)
If one enters the mosque while the athaan is being given:
Shaykh Ibn ‘Uthaymeen was asked: If I enter the mosque and the muezzin is giving the call to prayer, is it better to pray tahiyyat al-masjid or to repeat after the muezzin?
That depends. If you enter when the muezzin is giving the call for Jumu’ah prayer, the athaan that is given when the khateeb is on the minbar, in this case we say that you should hasten to pray Tahiyyat al-masjid, and you should not wait until the muezzin has finished, because preparing yourself to listen to the khutbah is more important than repeating after the muezzin, as listening to the khutbah is obligatory and responding to the muezzin is not obligatory.
But if the athaan is not for Jumu’ah prayer, then it is better for you to remain standing until you have responded to the muezzin, and offered the well-known du’aa’ after the athaan. Then after that you can pray Tahiyyat al-masjid. (Taken from https://islamqa.info/en/11960)