Burayda bin Husayb reported,

The Messenger of Allah ﷺ used to teach them what to say when they went out to the graveyards. The one who spoke among them would say (in the narration of Abu Bakr), "Peace be upon the inhabitants of these dwellings," and (in the narration of Zuhayr), "Peace be upon you, inhabitants of these dwellings, from among the believers and the Muslims. We, Allah willing, shall be joining you. I ask Allah for well-being for us and for you."

Azami 3902

Sahih

عن بريدة بن الحصيب قال

كان رسول الله ﷺ يعلمهم إذا خرجوا إلى المقابر، فكان قائلهم يقول ( في رواية أبي بكر): السلام على أهل الديار، ( وفي رواية زهير): السلام عليكم أهل الديار من المؤمنين والمسلمين، وإنا إن شاء الله للاحقون، أسأل الله لنا العافية.

الأعظمي 3902صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Funerals
Chapter
Visiting graves
Section
Supplications for the people of the graves and seeking forgiveness for the people of Baqi
Numbering
Jami al-Kamil 3902 (2nd edition)
Azami 3902