The Messenger of Allah ﷺ prayed toward Bayt al-Maqdis for sixteen or seventeen months, though he wished his prayer direction would be toward the Kabah. He prayed the afternoon prayer, and a group of people prayed with him.One of those who had prayed with him went out and passed by the people of 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, toward the Kabah.Among those who had died facing the former prayer direction, before it was changed to the Kabah, were some men who had been killed, and we did not know what to say about them. Then Allah revealed, "And never would Allah have caused your faith to be lost. Indeed Allah is, to the people, Kind and Merciful" [Baqarah 2:143].
أن رسول الله ﷺ صلى إلى بيت المقدس ستة عشر شهرا، أو: سبعة عشر شهرا، وكان يعجبه أن تكون قبلته قبل البيت، وأنه صلى أو صلاها صلاة العصر، وصلى معه قوم، فخرج رجل ممن كان صلى معه، فمر على أهل المسجد وهم راكعون، قال: أشهد بالله لقد صليت مع النبي ﷺ قبل مكة، فداروا كما هم قبل البيت، وكان الذي مات على القبلة قبل أن تحول قبل البيت رجال قتلوا لم ندر ما نقول فيهم، فأنزل الله: {وما كان الله ليضيع إيمنكم إن الله بالناس لرءوف رحيم}.
Bukhari narrated it in the Book of Tafsir (4486) and Muslim in the Book of Mosques (525), both from the hadith of Abu Ishaq, from Bara bin Azib, who related it, and the wording is Bukhari's. His statement, "your faith" [Baqarah 2:143], means your prayer.