Praise be to Allah.
There are differences among the scholars concerning this matter. The correct opinion is that Tahiyat al-masjid is permitted at all times, even after Fajr and after ‘Asr, because of the general applicability of the hadeeth “When any one of you enters the Mosque, he should not sit down until he has prayed two rak’ahs”
(Saheeh – agreed upon), and because this is one of the prayers that is done for a specific reason, like Salaat al-Tawaaf (prayer done after circumambulating the Ka’bah) and Salaat al-Kusoof (prayer at the time of an eclipse). The correct view concerning all of these is that it is permissible to do these at times when prayer is generally disallowed, like making up missed prayers. Concerning Salaat al-Tawaaf, the Prophet (peace and blessings of Allaah be upon him) said: “O Banu ‘Abd Manaaf, do not prevent anyone from circumambulating this House and praying at any time of night or day.” (Reported by Imaam Ahmad and the authors of Sunan with a saheeh isnaad); concerning Salaat al-Kusoof, he (peace and blessings of Allaah be upon him) said: “The sun and moon are two of the Signs of Allaah; they are not eclipsed because of the death or life of anyone.” (1/332).