Reference: al-Muntaqaa min-Fataawa al-Fowzaan v4 p53
Question: Are the two rak'ahs of tahiyyatul-masjid obligatory for everyone who enters the masjid, even if one wishes to perform the obligatory prayer immediately?
Response:
So whoever enters the masjid and wishes to sit down should perform two rak'ahs before doing so. As for he who enters the masjid as he passes by it and does not want to sit down but rather, he needs something after which he will leave, then he is not required to pray two rak'ahs. Likewise, if he entered to perform the obligatory prayer and finds that the prayer (jamaa'ah) has already started, he should join the prayer with the jamaa'ah and this suffices for tahiyyatul-masjid.
