All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
When one enters the mosque and meets someone on his way to the place where he was heading to perform prayer, he should greet him first since greeting fellow Muslims is one of their rights over a Muslim. The Prophet, sallallaahu ‘alayhi wa sallam, said: “A Muslim has six rights over another Muslim.” They asked, “What are they O Messenger of Allaah?” He, sallallaahu ‘alayhi wa sallam, said: “When you meet him, greet him with Salaam (i.e. Muslim's salutation meaning ‘peace’); when he invites you, accept his invitation; when he seeks your advice, advise him; when he sneezes and then praises Allaah, invoke the Mercy of Allaah upon him; when he falls ill, visit him; and when he dies, follow his funeral.” [Al-Bukhari, Muslim, Ahmad, At-Tirmithi and others]
However, if one did not meet anyone while entering the mosque, he should head towards a pillar in the mosque or to the first row and perform the prayer of Tahiyyat-ul-Masjid. The Prophet, sallallaahu ‘alayhi wa sallam, said: "When one of you comes to the mosque, he should pray two Rak‘ahs before he sits." [Al-Bukhari, Muslim, Abu Daawood, An-Nisaa’i, At-Tirmithi and Ibn Maajah]
Allaah Knows best.