Ibn 'Abbas said that when God's messenger passed by some graves in Medina he turned his face towards them and said, “Peace be upon you, inhabitants of the graves. God forgive us and you. You have gone before us and we are coming after.”
Tirmidhi transmitted it saying this is a hasan gharib tradition.
Buraida said that God's messenger used to teach them to say when they went out to the cemeteries, “Peace be upon you, inhabitants of the dwellings who are of the number of the believers and the Muslims. If God will, we shall join you. We ask God to grant us and you wellbeing.”
‘A'isha said that whenever it was her turn for God's messenger to spend the night with her he would go out towards the end of the night to al-Baqi' and say, “Peace be upon you, household of a people who are believers. What you were being promised will come to you tomorrow, you receiving it after some delay, and if God will we shall join you. O God, grant forgiveness to the inhabitants of Baqi' al- Gharqad.” (This is the full name of the cemetery at Medina. Baqi' properly means a place with tree roots, and gharqad is the name of a thorny type of tree. The name was retained even when all signs of trees were removed.)
Abu Huraira said that the Prophet visited his mother’s grave and wept and caused those around him to weep. He then said, "I asked my Lord's permission to pray for forgiveness for her, but I was not allowed; I then asked His permission to visit her grave and I was allowed; so visit graves, for they make one mindful of death.”