Bara bin Azib reported,

The Messenger of Allah ﷺ prayed facing Bayt al-Maqdis for sixteen or seventeen months, but he wished that his prayer direction would be toward the Kaaba. He prayed, or led, the afternoon prayer, and a group of people prayed with him. One of those who had prayed with him went out and passed by some people in a mosque while they were bowing in prayer. He said, "I bear witness by Allah that I have just prayed with the Prophet ﷺ facing Mecca." So they turned, just as they were, to face the Kaaba.Some men who had died while still praying toward the original direction, before it was changed to the Kaaba, had been killed, and we did not know what to say about them. Then Allah revealed, "And Allah would never let your faith go to waste. Indeed, Allah is Most Kind and Merciful to people" [Baqarah 2:143].

Bukhari 4486

Sahih

حدثنا أبو نعيم سمع زهيرا عن أبي إسحاق

عن البراء رضي الله عنه

أن رسول الله ﷺ صلى إلى بيت المقدس ستة عشر شهرا أو سبعة عشر شهرا وكان يعجبه أن تكون قبلته قبل البيت وأنه صلى أو صلاها صلاة العصر وصلى معه قوم فخرج رجل ممن كان صلى معه فمر على أهل المسجد وهم راكعون قال أشهد بالله لقد صليت مع النبي ﷺ قبل مكة فداروا كما هم قبل البيت وكان الذي مات على القبلة قبل أن تحول قبل البيت رجال قتلوا لم ندر ما نقول فيهم فأنزل الله {وما كان الله ليضيع إيمانكم إن الله بالناس لرءوف رحيم}

البخاري 4486صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Tafsir
Chapter
Foolish among the people will say, "What has turned them away from the qiblah they used to face?" Say, "To Allah belongs the east and the Maghrib. He guides whom He wills to a straight path."
Numbering
Darussalam 4486
Bukhari 4486