Praise be to Allah.
It is permissible to visit the graves of those who did not die in Islam in order only to learn a lesson from that.
This is because of the report of Abu Hurayrah, who said: “The Prophet (peace and blessings of Allaah be upon him) visited the grave of his mother, and he wept and those who were with him wept. Then he said, ‘I asked my Lord for permission to pray for forgiveness for her, and He did not grant me permission to do that, and I asked Him for permission to visit her grave, and He gave me permission. So visit the graves, for they are a reminder of death.’”
(Narrated by Muslim, 3/65; Abu Dawood, 2/72; al-Nasaa’i, 1/286; also by Ibn Maajah, al-Haakim, al-Bayhaqi and Ahmad)
Buraydah (may Allaah be pleased with him) narrated: “We were with the Prophet (peace and blessings of Allaah be upon him) on a journey (according to one report, on the campaign of the conquest [of Makkah]), and he camped with us, and we were nearly one thousand on mounts. He prayed two rak’ahs, then he turned to us weeping. ‘Umar ibn al-Khattaab (may Allaah be pleased with him) got up and went to him, and said, ‘May my father and mother be sacrificed for you, O Messenger of Allaah, what is the matter?’ He said, ‘I asked my Lord to let me pray for forgiveness for my mother, and He did not give me permission, and my eyes filled with tears out of compassion for her in the Fire. I asked my Lord for permission to visit her grave, and He granted me permission. I used to forbid you to visit the graves, but (now) visit them, for visiting them will increase you in good.’”
(Narrated by Ahmad, 5/355-359; Ibn Abi Shaybah, 4/139; also narrated by Ibn Hibbaan and al-Haakim. The other addition was narrated by al-Haakim who said, it is saheeh according to the conditions of the two Shaykhs [al-Bukhaari and Muslim], and al-Dhahabi agreed with him, and it is as they said. A briefer version was narrated by al-Tirmidhi, who classed it as saheeh).
If a Muslim visits the grave of a kaafir, he does not greet h m with salaams or make du’aa’ for him; rather he should give him the tidings of Hell.
The evidence for that is the hadeeth of Sa’d ibn Abi Waqqaas who said: “A Bedouin came to the Prophet (peace and blessings of Allaah be upon him) and said, ‘My father used to uphold the ties of kinship, and so on and so forth - where is he now?’ He said, ‘In Hell.’ The Bedouin got upset and said, ‘O Messenger of Allaah, where is your father?’ He said, ‘Whenever you pass by the grave of a kaafir, give him the tidings of Hell.’ The Bedouin later became a Muslim, and he said, ‘The Messenger of Allaah (peace and blessings of Allaah be upon him) gave me a difficult commission. Whenever I pass by the grave of a kaafir, I give him the tidings of Hell.”’”
(Narrated by al-Tabaraani in al-Mu’jam al-Kabeer, 1/191; Ibn al-Sunni in ‘Aml al-Yawm wa’l-Laylah, 588; al-Diyaa’ al-Maqdisi in al-Ahaadeeth al-Mukhtaarah, with a saheeh isnaad. Al-Haythami (1/117-118) said: it was narrated by al-Bazzaar and by al-Tabaraani in al-Kabeer, and the men of its isnaad are sound).
And Abu Hurayrah narrated in a marfoo’ hadeeth [i.e., it is attributed to the Prophet (peace and blessings of Allaah be upon him)]: “When you pass by the graves of the people of the jaahilyyah, of our people and your people, give them the news that they are among the people of Hell.” (Narrated by Ibn al-Sunni in al-Yawm wa’l-Laylah with an isnaad which includes Yahyaa ibn Yamaan, who had a bad memory, from Muhammad ibn ‘Umar. I do not know it from Abu Salamah from him, but apparently he is Ibn ‘Amr, who is hasan in narrating hadeeth. And Allaah knows best.”