What is the ruling on visiting graves?
Visiting graves is a Sunnah that the Prophet (blessings and peace of Allah be upon him) enjoined and did himself. It was narrated from `Abdullah ibn Buraydah that his father (may Allah be pleased with him) said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “I used to forbid you to visit graves, but now visit them.” Narrated by Muslim. According to a version narrated by at-Tirmidhi (1054), “for they are a reminder of the hereafter.”
Ibn `Abd al-Barr (may Allah have mercy on him) said: In terms of jurisprudence, from this hadith we learn that it is permissible to go out to the graveyard and visit the graves, and this is a matter concerning which there is scholarly consensus in the case of men, but there is a difference of opinion in the case of women. It is only narrated from the Prophet (blessings and peace of Allah be upon him) that he said: “I used to forbid you to visit graves, but now visit them and do not say anything improper, for they are a reminder of the hereafter.”
(End quote from at-Tamhid, 20/239).
It was narrated that `A’ishah (may Allah be pleased with her) said: The Messenger of Allah (blessings and peace of Allah be upon him) used to go out to al-Baqi`, and he would say: “Peace be upon you, O abode of believing people. What you were promised will come to you soon, after some delay, and we – if Allah wills – will join you soon. O Allah, forgive the people of Baqi` al-Gharqad.” Narrated by Muslim, 974.
An-Nawawi (may Allah have mercy on him) said: The texts of ash-Shafa`i and the our companions are all agreed that it is recommended for men to visit the graves, and this is the view of all the scholars; al-`Abdari narrated that the Muslims are unanimously agreed on that, and the evidence for that, in addition to scholarly consensus, is the well-known authentic hadiths. Visiting graves was initially forbidden, then that was abrogated.
(End quote from Sharh al-Muhadhdhab, 5/284)
It says in the words of the Permanent Committee (9/103): It is Sunnah for men to visit graves, because the Prophet (blessings and peace of Allah be upon him) did that and enjoined others to do it, and because that was the action of the Rightly-Guided Caliphs and all the Companions (may Allah be pleased with them), and the leading scholars of the Muslims, with no difference of opinion, so there is consensus, and because the Prophet (blessings and peace of Allah be upon him) said: “I used to forbid you to visit graves, but now visit them…” End quote.
As-San`ani (may Allah have mercy on him) said: It is a matter that is recommended, according to scholarly consensus, and it is emphasized in the case of parents, because of the reports which speak of that.
(End quote from Subul as-Salam, 2/114)
Regarding the reason why it is prescribed to visit graves, Shaykh `Abdullah al-Ghunayman said: It is prescribed to visit graves for two reasons:
The first reason is that it will remind a person of his ultimate fate, because he will end up as the occupant of the grave has ended up, and his destination will be the grave, and he will inevitably be buried like this person. So let him repent, strive hard in doing good deeds, remember the hereafter and remember what lies ahead of him.
The second reason is that he can show kindness to the deceased by offering supplication for him, as he is in the greatest need of supplication from a sincere brother of his that will benefit him. But if the matter is reversed, and the person visits the grave to ask its occupant for help, this is the exact opposite of what the Messenger (blessings and peace of Allah be upon him) meant.
(End quote from Sharh Fat-h al-Majid)
The scenario in which it is recommended to visit graves is when the grave is in the same city as the one who visits it; but if it is so far away from the city that going out to visit it would be described as travel, then it is not prescribed to visit it; rather it is prohibited.
For more information, please see the answer to question no. 10011.
And Allah knows best.