Short answer
Whoever attends a Muslim's funeral until the prayer is done earns one qirat of reward, and whoever stays until the burial earns two. The Prophet ﷺ said each qirat is like a great mountain. Attending is also one of the rights a Muslim has over another.
The janazah prayer is fard kifayah on the community, and following the funeral to the grave is sunnah. Attending the janazah of any Muslim, known or unknown, is a rewarded act.
The four schools agree on this.
Explanation
Going to a janazah takes little time and carries great reward. You pray for a few minutes, walk with the body, and help with the burial. In return Allah gives a reward the Prophet ﷺ compared to mountains. The four schools agree on its virtue.
- One qirat for attending until the prayer is finished.
- Two qirats for staying until the body is buried. When asked what the two qirats are, he said: like two great mountains.
- Intercession. If forty Muslims who associate nothing with Allah pray over a person, Allah accepts their intercession for him. A large, sincere attendance is a gift to the dead.
- A right of your brother. Following funerals is one of the five rights a Muslim has over another.
- A reminder. Seeing a grave dug and filled softens the heart and turns it to the hereafter.
Do I need to know the person? No. Many Muslims attend the janazah of strangers at their local mosque, especially of people with little family nearby. Your presence comforts the family and adds to those praying for the dead.
For the steps of the prayer, see how to pray janazah. For how to walk with the body, see the etiquette of following a janazah. For women and the procession, see can women attend a janazah.
The evidence
Quran, Surah Al-Hashr 59:10
وَٱلَّذِينَ جَآءُو مِنۢ بَعْدِهِمْ يَقُولُونَ رَبَّنَا ٱغْفِرْ لَنَا وَلِإِخْوَٰنِنَا ٱلَّذِينَ سَبَقُونَا بِٱلْإِيمَٰنِ وَلَا تَجْعَلْ فِى قُلُوبِنَا غِلًّۭا لِّلَّذِينَ ءَامَنُوا۟ رَبَّنَآ إِنَّكَ رَءُوفٌۭ رَّحِيمٌ ﴿١٠﴾
And [there is a share for] those who came after them, saying, "Our Lord, forgive us and our brothers who preceded us in faith and put not in our hearts [any] resentment toward those who have believed. Our Lord, indeed You are Kind and Merciful."
Praying for those who died before us is part of faith.
Sahih al-Bukhari 1325
Sahih... من شهد الجنازة حتى يصلى عليها فله قيراط ...
"Whoever attends the funeral until the prayer is done has one qirat."
This is the reward for attending the prayer.
Sahih Muslim 945
Sahih... ومن شهدها حتى تدفن فله قيراطان ...
"And whoever attends it until the burial has two qirats."
Staying to the burial doubles the reward.
Sahih Muslim 948
Sahih... ما من رجل مسلم يموت فيقوم على جنازته أربعون رجلا ...
"No Muslim dies and forty men stand in prayer over him, associating nothing with Allah, except that Allah accepts their intercession for him."
The prayers of many believers benefit the dead.
Where the ruling is recorded
- al-Hidaya, Kitab al-Salah, Bab al-Jana'iz. The janazah as a communal duty and following the bier.
Checked by our team. For a more accurate answer to your own situation, consult a scholar. Quran translation by Sahih International. Hadith Arabic from the standard editions of the six books and the Muwatta, with gradings as recorded there; the English is our own rendering. Spotted a mistake? Tell us.