Saying salaam when entering the mosque
Question

I hope that you could explain which is more correct, saying salaam when entering the mosque, or offering the prayer to greet the mosque first, with evidence?
Praise be to Allah.Firstly:
The one who enters a mosque should pray two rak`ahs, or more if he wishes, when he first enters the mosque. These are the two rak’ahs that are known as tahiyat al-masjid (greeting the mosque), because the Prophet (blessings and peace of Allah be upon him) said: “When one of you enters the mosque, let him pray two rak’ahs before he sits down.” Narrated by al-Bukhari, 1176; Muslim, 714.
For more information, please see the answer to question no. 181099.
Secondly:
There is no contradiction between saying salaam when entering the mosque and offering the prayer to greet the mosque. The one who enters the mosque can say salaam to anyone whom he meets when he enters, then he can start to offer the prayer to greet the mosque.
It should be noted that greeting the mosque is connected to sitting down, so there is nothing wrong with delaying it for a short time, if the person is distracted by something, so long as he does not sit down.
As for greeting a Muslim, that is connected to meeting him, so when he meets him he should greet him with salaam.
It was narrated from Abu Hurayrah (may Allah be pleased with him) that the Messenger of Allah (blessings and peace of Allah be upon him) said: “When one of you meets his brother and greets him with salaam, then a tree, wall or rock comes in between them, then he meets him again, let him greet him with salaam again.” Narrated by Abu Dawud, 5200; classed as authentic by al-Albani.
It was narrated from at-Tufayl ibn Ubayy ibn Ka`b that he used to come to `Abdullah bin `Umar (may Allah be pleased with them) in the morning and go with him to the market. He said: When we went in the morning to the market, ‘Abdullah would not pass any seller of inferior merchandise, trader, poor person or anyone else, but he would greet him with salaam. At-Tufayl said: I came to `Abdullah ibn `Umar one day, and he asked me to accompany him to the market. I said to him: Why do you go to the market, when you do not sell, or ask about items, or offer a price for them, or sit with any gathering in the market? I say: let us sit down here and talk. He said: O Abu Batn (father of a belly) – as at-Tufayl had a large belly – we only go to the market in the morning to greet with salaam everyone we meet. Narrated by Malik in al-Muwatta’, 2763; an-Nawawi classed its isnad as authentic in Riyadh as-Salihin.
So if the congregation of the mosque, or the one whom he wants to greet with salaam, is in some corner of the mosque, then he may pray first, then go to them and greet them with salaam, if he wishes.
It was narrated from Abu Hurayrah (may Allah be pleased with him) – in the hadith about the one who prayed badly – that that man came and prayed, then he came to the Prophet (blessings and peace of Allah be upon him) and greeted him with salaam; he returned his greeting, then said: “Go back and pray, for you have not prayed.” So he went back and prayed, then he came and greeted the Prophet (blessings and peace of Allah be upon him) with salaam, and he did that three times. Al-Bukhari and Muslim.
And Allah knows best.