Language:
كتاب الجنائز
23  
Funerals (Al-Janaa'iz)
(32)
Chapter: The statement of the Prophet ﷺ : "The deceased is punished because of the weeping (with wailing) of some of his relatives, if wailing was the custom of that dead person."
(32)
باب قَوْلِ النَّبِيِّ صلى الله عليه وسلم: «يُعَذَّبُ الْمَيِّتُ بِبَعْضِ بُكَاءِ أَهْلِهِ عَلَيْهِ» إِذَا كَانَ النَّوْحُ مِنْ سُنَّتِهِ
Sahih al-Bukhari 1284

Narrated Usama bin Zaid:

The daughter of the Prophet (p.b.u.h) sent (a messenger) to the Prophet (ﷺ) requesting him to come as her child was dying (or was gasping), but the Prophet (ﷺ) returned the messenger and told him to convey his greeting to her and say: "Whatever Allah takes is for Him and whatever He gives, is for Him, and everything with Him has a limited fixed term (in this world) and so she should be patient and hope for Allah's reward." She again sent for him, swearing that he should come. The Prophet (ﷺ) got up, and so did Sa`d bin 'Ubada, Mu`adh bin Jabal, Ubai bin Ka`b, Zaid bin Thabit and some other men. The child was brought to Allah's Messenger (ﷺ) while his breath was disturbed in his chest (the sub-narrator thinks that Usama added: ) as if it was a leather water-skin. On that the eyes of the Prophet (p.b.u.h) started shedding tears. Sa`d said, "O Allah's Messenger (ﷺ)! What is this?" He replied, "It is mercy which Allah has lodged in the hearts of His slaves, and Allah is merciful only to those of His slaves who are merciful (to others).

حَدَّثَنَا عَبْدَانُ، وَمُحَمَّدٌ، قَالاَ أَخْبَرَنَا عَبْدُ اللَّهِ، أَخْبَرَنَا عَاصِمُ بْنُ سُلَيْمَانَ، عَنْ أَبِي عُثْمَانَ، قَالَ حَدَّثَنِي أُسَامَةُ بْنُ زَيْدٍ ـ رضى الله عنهما ـ قَالَ أَرْسَلَتِ ابْنَةُ النَّبِيِّ صلى الله عليه وسلم إِلَيْهِ إِنَّ ابْنًا لِي قُبِضَ فَائْتِنَا‏.‏ فَأَرْسَلَ يُقْرِئُ السَّلاَمَ وَيَقُولُ ‏"‏ إِنَّ لِلَّهِ مَا أَخَذَ وَلَهُ مَا أَعْطَى وَكُلٌّ عِنْدَهُ بِأَجَلٍ مُسَمًّى، فَلْتَصْبِرْ وَلْتَحْتَسِبْ ‏"‏‏.‏ فَأَرْسَلَتْ إِلَيْهِ تُقْسِمُ عَلَيْهِ لَيَأْتِيَنَّهَا، فَقَامَ وَمَعَهُ سَعْدُ بْنُ عُبَادَةَ وَمُعَاذُ بْنُ جَبَلٍ وَأُبَىُّ بْنُ كَعْبٍ وَزَيْدُ بْنُ ثَابِتٍ وَرِجَالٌ، فَرُفِعَ إِلَى رَسُولِ اللَّهِ صلى الله عليه وسلم الصَّبِيُّ وَنَفْسُهُ تَتَقَعْقَعُ ـ قَالَ حَسِبْتُهُ أَنَّهُ قَالَ ـ كَأَنَّهَا شَنٌّ‏.‏ فَفَاضَتْ عَيْنَاهُ‏.‏ فَقَالَ سَعْدٌ يَا رَسُولَ اللَّهِ مَا هَذَا فَقَالَ ‏"‏ هَذِهِ رَحْمَةٌ جَعَلَهَا اللَّهُ فِي قُلُوبِ عِبَادِهِ، وَإِنَّمَا يَرْحَمُ اللَّهُ مِنْ عِبَادِهِ الرُّحَمَاءَ ‏"‏‏.‏
Reference : Sahih al-Bukhari 1284
In-book reference : Book 23, Hadith 44
USC-MSA web (English) reference : Vol. 2, Book 23, Hadith 373
  (deprecated numbering scheme)