Praise be to Allaah.
It is prescribed to visit graves in order to learn a lesson from that and to remember the Hereafter. That is subject to the condition that one does not say anything that will anger the Lord, such as calling upon the one who is buried or seeking his help instead of Allaah, or praising him and saying that he is for certain in Paradise, etc.
The purpose of visiting the graves is twofold:
(a) The visitor benefits from remembering death and the dead, remembering that their destiny will be either Paradise or Hell. This is the primary purpose of the visit.
(b) The deceased also benefits and is treated kindly by the visitor greeting him with salaams, making du’aa’ for him, praying for forgiveness for him. This applies only to Muslims. Among the du’aa’s that may be recited are:
“Assalaamu ‘alaykum ahl al-diyaar min al-mu’mineen wa’l-Muslimeen, in sha Allaah bikum laahiqoon, as’al Allaaha lana wa lakum al-‘aafiyah (peace be upon you O people of the dwellings, believers and Muslims, In sha Allaah we will join you, I ask Allaah to keep us and you safe and sound).”
It is permissible to raise the hands when reciting this du’aa’, because of the hadeeth of ‘Aa’ishah who said: “The Messenger of Allaah (peace and blessings of Allaah be upon him) went out one night, and I sent Bareerah to follow him and see where he went. She said, ‘He went towards Baqee’ al-Gharqad [the graveyard in Madeenah], and he stood at the bottom of al-Baqee’ and raised his hands, then he went away.’ Bareerah came back to me and told me, and when morning came I asked him about it. I said, ‘O Messenger of Allaah, where did you go out to last night? He said, ‘I was sent to the people of al-Baqee’, to pray for them.’”
But you should not face the grave when making du’aa’ for them; rather you should face the direction of the Ka’bah, because the Prophet (peace and blessings of Allaah be upon him) forbade prayer (salaah) facing graves, and du’aa’ is the heart and soul of salaah, as is well known, and is subject to the same rulings. The Prophet (peace and blessings of Allaah be upon him) said: “Du’aa’ is worship” then he recited the aayah (interpretation of the meaning): “And your Lord said: “Invoke Me [i.e. believe in My Oneness (Islamic Monotheism) and ask Me for anything] I will respond to your (invocation).” [Ghaafir 40:60]
You should not walk between the graves of the Muslims wearing your shoes. It was narrated that ‘Uqbah ibn ‘Aamir (may Allaah be pleased with him) said: “The Messenger of Allaah (peace and blessings of Allaah be upon him) said: ‘If I were to walk on hot coals or on a sword, or if I were to mend my shoes using my feet, that would be better for me than if I were to walk on the grave of a Muslim. And it makes no difference to me if I were to relieve myself in the midst of the graves or in the middle of the market-place [i.e., both are equally bad].’” (Narrated by Ibn Maajah, 1567)
We ask Allaah, the Most High, the Omnipotent, to have mercy upon our dead and the deceased Muslims.
…Abide by the truth even though your mind deserts you.
[Adapted from Mukhtasar Ahkaam al-Janaa’iz by al-Albaani]