The Prophet—may blessings and peace be upon him—said: “Three things accompany the dead man: his wealth, his family, and his deeds. Two turn back and one remains; his family and his wealth go back, while his knowledge stays with him.”
Reflect, then, on these people, and greet them saying: “Peace be upon you, O abode of believing folk.” It appears—and Allah knows best—that they return the greeting, for he addresses them directly: “Peace be upon you.” It is also possible that the greeting is meant merely as a supplication for them, whether they hear it or not, respond or do not respond.
In every case one should pray for them and affirm the inevitable outcome by saying: “And surely, if Allah wills, we shall join you.” The phrase “if Allah wills” refers to the time of that joining, not to the joining itself; the joining is certain, and what is certain is not made conditional on will. Rather, it is the timing that is subject to Allah’s will, for none of us knows when he will join them. Thus the meaning is: “Whenever Allah wills, we shall join you,” just as He says: “Then, when He wills, He will resurrect him” and “No! He has not yet carried out what He commanded him” (Qur’an 80:22–23).
After that he should invoke the supplications established in the sunnah; if he does not know any of them, he may say whatever is easy: “O Allah, forgive them. O Allah, have mercy on them. O Allah, do not deprive us of their reward, do not test us after them, and forgive us and them.” He then departs. This was how the Messenger, peace and blessings be upon him, used to visit the cemetery.