Sahih al-Bukhari, Hadith 4486

Narrated Al-Bara: The Prophet prayed facing Bait-ulMaqdis (i.e. Jerusalem) for sixteen or seventeen months but he wished that his Qibla would be the Ka`ba (at Mecca). (So Allah Revealed (2.144) and he offered `Asr prayers(in his Mosque facing Ka`ba at Mecca) and some people prayed with him. A man from among those who had prayed with him, went out and passed by some people offering prayer in another mosque, and they were in the state of bowing. He said, "I, (swearing by Allah,) testify that I have prayed with the Prophet facing Mecca." Hearing that, they turned their faces to the Ka`ba while they were still bowing. Some men had died before the Qibla was changed towards the Ka`ba. They had been killed and we did not know what to say about them (i.e. whether their prayers towards Jerusalem were accepted or not). So Allah revealed:-- "And Allah would never make your faith (i.e. prayer) to be lost (i.e. your prayers offered (towards Jerusalem). Truly Allah is Full of Pity, Most Merciful towards mankind." (2.143)

حَدَّثَنَا أَبُو نُعَيْمٍ، سَمِعَ زُهَيْرًا، عَنْ أَبِي إِسْحَاقَ، عَنِ الْبَرَاءِ، رضى الله عنه أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم صَلَّى إِلَى بَيْتِ الْمَقْدِسِ سِتَّةَ عَشَرَ شَهْرًا أَوْ سَبْعَةَ عَشَرَ شَهْرًا، وَكَانَ يُعْجِبُهُ أَنْ تَكُونَ قِبْلَتُهُ قِبَلَ الْبَيْتِ، وَإِنَّهُ صَلَّى ـ أَوْ صَلاَّهَا ـ صَلاَةَ الْعَصْرِ، وَصَلَّى مَعَهُ قَوْمٌ، فَخَرَجَ رَجُلٌ مِمَّنْ كَانَ صَلَّى مَعَهُ، فَمَرَّ عَلَى أَهْلِ الْمَسْجِدِ وَهُمْ رَاكِعُونَ قَالَ أَشْهَدُ بِاللَّهِ لَقَدْ صَلَّيْتُ مَعَ النَّبِيِّ صلى الله عليه وسلم قِبَلَ مَكَّةَ، فَدَارُوا كَمَا هُمْ قِبَلَ الْبَيْتِ، وَكَانَ الَّذِي مَاتَ عَلَى الْقِبْلَةِ قَبْلَ أَنْ تُحَوَّلَ قِبَلَ الْبَيْتِ رِجَالٌ قُتِلُوا لَمْ نَدْرِ مَا نَقُولُ فِيهِمْ، فَأَنْزَلَ اللَّهُ ‏{‏وَمَا كَانَ اللَّهُ لِيُضِيعَ إِيمَانَكُمْ إِنَّ اللَّهَ بِالنَّاسِ لَرَءُوفٌ رَحِيمٌ ‏}‏

sunnah.com