Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:14:27

Allah keeps firm those who believe, with the firm word, in worldly life and in the Hereafter. And Allah sends astray the wrongdoers. And Allah does what He wills.  

God confirms those who believe by a firm saying, namely, the profession of ˹His˺ Oneness, in the life of this world and in the Hereafter, that is, in the grave, when the two angels question them about their Lord, their religion and their Prophet, and they respond correctly, as ˹reported˺ in the hadīth of the two Shaykhs ˹alBukhārī and Muslim˺; and God sends astray the evildoers, the disbelievers, and therefore they are not guided to the correct response, saying instead, ‘We do not know’, as ˹reported˺ in the hadīth; and God does what He will.
القرآن:١٤:٢٧

يُثَبِّتُ اللَّهُ الَّذِينَ آمَنُوا بِالْقَوْلِ الثَّابِتِ فِي الْحَيَاةِ الدُّنْيَا وَفِي الْآخِرَةِ ۖ وَيُضِلُّ اللَّهُ الظَّالِمِينَ ۚ وَيَفْعَلُ اللَّهُ مَا يَشَاءُ  

{يثبت الله الذين آمنوا بالقول الثابت} هي كلمة التوحيد {في الحياة الدنيا وفي الآخرة} أي في القبر لما يسألهم الملكان عن ربهم ودينهم ونبيهم فيجيبون بالصواب كما في حديث الشيخين {ويضل الله الظالمين} الكفار فلا يهتدون للجواب بالصواب بل يقولون لا ندري كما في الحديث {ويفعل الله ما يشاء}.

See similar narrations below:

Collected by Nasāʾī, Tirmidhī
nasai:2056Isḥāq b. Manṣūr > ʿAbd al-Raḥman > Sufyān from his father > Khaythamah > al-Barāʾ

Al-Bara said about Allah will keep firm those who believe, with the word that stands firm in this world and in the Hereafter "It was revealed concerning the torment of the grave.  

النسائي:٢٠٥٦أَخْبَرَنَا إِسْحَاقُ بْنُ مَنْصُورٍ قَالَ حَدَّثَنَا عَبْدُ الرَّحْمَنِ عَنْ سُفْيَانَ عَنْ أَبِيهِ عَنْ خَيْثَمَةَ عَنِ الْبَرَاءِ قَالَ

{ يُثَبِّتُ اللَّهُ الَّذِينَ آمَنُوا بِالْقَوْلِ الثَّابِتِ فِي الْحَيَاةِ الدُّنْيَا وَفِي الآخِرَةِ } قَالَ نَزَلَتْ فِي عَذَابِ الْقَبْرِ  

tirmidhi:3120Maḥmūd b. Ghaylān > Abū Dāwud > Shuʿbah > ʿAlqamah b. Marthid > Saʿd b. ʿUbaydah > al-Barāʾ

that regarding Allah's saying: Allah will keep firm those who believe, with the word that stands firm in this world and in the Hereafter (14:27). The Prophet ﷺ said: "In the grave, when it is said to him: 'Who is your Lord? What is your religion? And who is your Prophet?'"  

Abū ʿĪsá said that this Ḥadīth is Ḥasan-Ṣaḥīḥ.
الترمذي:٣١٢٠حَدَّثَنَا مَحْمُودُ بْنُ غَيْلاَنَ حَدَّثَنَا أَبُو دَاوُدَ حَدَّثَنَا شُعْبَةُ أَخْبَرَنِي عَلْقَمَةُ بْنُ مَرْثِدٍ قَالَ سَمِعْتُ سَعْدَ بْنَ عُبَيْدَةَ يُحَدِّثُ عَنِ الْبَرَاءِ

عَنِ النَّبِيِّ ﷺ فِي قَوْلِ اللَّهِ ( يُثَبِّتُ اللَّهُ الَّذِينَ آمَنُوا بِالْقَوْلِ الثَّابِتِ فِي الْحَيَاةِ الدُّنْيَا وَفِي الآخِرَةِ ) قَالَ فِي الْقَبْرِ إِذَا قِيلَ لَهُ مَنْ رَبُّكَ وَمَا دِينُكَ وَمَنْ نَبِيُّكَ؟  

قَالَ أَبُو عِيسَى هَذَا حَدِيثٌ حَسَنٌ صَحِيحٌ