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 is His slave and Messenger.
It is recommended for any person in a state of ablution who enters a mosque and wants to sit therein to perform a two-rakʻah (unit of prayer) tahiyyat al-masjid (the prayer of greeting the mosque) upon entering it. Sharh ʻAbd Al-Baaqi Az-Zarqaani ʻala Mukhtasar Khaleel Al-Maaliki reads, "It is recommended to perform tahiyyat al-masjid upon entering the mosque while in a state ablution at a time when performing prayer is permissible and the person intends to stay therein for a while..."
The Kuwaiti Encyclopedia of Fiqh reads:
"The majority of the scholars held that it is recommended for the one who enters a mosque, other than Al-Masjid Al-Haraam (the Ka'bah) - with the intention of staying therein, not just pass through it - and he is in a state of ablution, to perform two rakʻahs or more before sitting down. Abu Qataadah narrated that the Prophet said, 'When anyone of you enters the mosque, let him not sit down until he performs two rakʻahs.'"
Hence, it is not obligatory to perform tahiyyat al-masjid in the first place, nor is it recommended for a person who is merely passing by a mosque either inside a car or on foot. Tahiyyat al-masjid is intended to be a sort of greeting to Allaah, The Exalted, upon entering His house, i.e. the mosque. The Maaliki book Haashiyat Ad-Dusooqi reads, "The person performing tahiyyat al-masjid should perform it with the intention of drawing closer to Allaah and not for the mosque. It is called tahiyyat al-masjid meaning a greeting for the Lord of the mosque and not the mosque itself because when a person enters a house, he greets the owner of the house and not the house."
It is prescribed for the person passing by a graveyard to greet the dead buried therein, but it is not obligatory. Shaykh Ibn ʻUthaymeen was asked, "When we pass by a graveyard, should we greet the dead buried therein?" He answered:
"Yes, the scholars advised that it is prescribed to greet the dead buried in the graveyard whether the person passes by it or stops by it. However, it is better to stop, greet the dead buried therein, and recite the reported supplication. People mostly pass by the graveyards in their cars nowadays, which happens quite fast; the passerby would only recite one-fourth of the reported supplication while passing by the graveyard. If the graveyard is encircled with a fence that the passerby cannot see the graves, then this is not considered a visit for a graveyard." [Majmooʻ Al-Fataawa]
The rationale behind the prescribed greeting of a graveyard is to supplicate Allaah in favor of the dead therein. There is no doubt that these supplications reach and benefit them. Buraydah said:
"The Prophet used to teach them (i.e., the Companions), whenever they came out to visit the graveyard, to say, 'As-salamu ʻalaykum ahlad-diyyaari min al-mu'mineena wal-Muslimeen, wa inna in shaa' Allaahu bikum lahiqoon. Asʻalullaaha lana wa-lakum al-ʻaafiyah.'" (Peace be on you, O residents of the abodes of the believers and the Muslims, and if Allaah pleases, we shall follow you; we implore Allaah for well-being for ourselves and for you.) [Muslim and others]
As-Sanʻaani wrote:
"The fact that the Prophet asked Allaah, The Exalted, for well-being in this context indicates that it is one of the important and most noble things to ask Allaah for. Well-being as far as the dead are concerned means safeguarding them from the punishment and discussing their good and bad deeds on the Day of Judgment. The intent of visiting the graveyard is to supplicate Allaah in favor of the dead buried therein, remember the Hereafter, and declare renouncing the worldly pleasures..." [Subl As-Salaam]
Allaah knows best.