He—peace and blessings be upon him—would go out to al-Baqīʿ, greet the deceased Muslims, and pray for them. Such a visit is exclusive to Muslims, just as the funeral prayer is confined to the believers.
It is authentically reported from him that he said:
“May Allah curse the Jews and the Christians; they turned the graves of their prophets into places of worship.”
He was warning against what they had done. ʿĀʾishah said, “Had it not been for that, his grave would have been left in the open, but he disliked that it be made into a mosque.”
In another sound narration he was told about a church in Abyssinia, famed for its beauty and the images inside it. He remarked, “Whenever a righteous man among them died, they built a place of worship over his grave and put those images in it. Such people are the worst of Allah’s creation on the Day of Resurrection.” This report is found in the Ṣaḥīḥ collections.
Ṣaḥīḥ Muslim also records that Jundub ibn ʿAbd Allāh said: I heard the Prophet ﷺ five days before he passed away saying, “I declare before Allah that I take none of you as an intimate friend, for Allah has taken me as His close friend just as He took Abraham as His close friend. Had I taken an intimate friend from my community, I would have taken Abū Bakr. Beware: those before you used to turn the graves of their prophets into places of worship. Beware: do not take graves as places of worship; I forbid you to do so.”
In the Sunan collections he said, “Do not turn my grave into a festival; and send blessings upon me wherever you may be, for your blessings reach me.”
And in al-Muwaṭṭaʾ and elsewhere he ﷺ prayed, “O Allah, do not let my grave become an idol that is worshipped. Allah’s wrath was intensified against a people who made the graves of their prophets into places of worship.”