Language:
كتاب الإيمان
2  
Belief
(30)
Chapter: The (offering of) Salat (prayers) is a part of faith
(30)
باب الصَّلاَةُ مِنَ الإِيمَانِ
Sahih al-Bukhari 40

Narrated Al-Bara' (bin 'Azib):

When the Prophet (ﷺ) came to Medina, he stayed first with his grandfathers or maternal uncles from Ansar. He offered his prayers facing Baitul-Maqdis (Jerusalem) for sixteen or seventeen months, but he wished that he could pray facing the Ka'ba (at Mecca). The first prayer which he offered facing the Ka'ba was the 'Asr prayer in the company of some people. Then one of those who had offered that prayer with him came out and passed by some people in a mosque who were bowing during their prayers (facing Jerusalem). He said addressing them, "By Allah, I testify that I have prayed with Allah's Messenger (ﷺ) facing Mecca (Ka'ba).' Hearing that, those people changed their direction towards the Ka'ba immediately. Jews and the people of the scriptures used to be pleased to see the Prophet (ﷺ) facing Jerusalem in prayers but when he changed his direction towards the Ka'ba, during the prayers, they disapproved of it.

Al-Bara' added, "Before we changed our direction towards the Ka'ba (Mecca) in prayers, some Muslims had died or had been killed and we did not know what to say about them (regarding their prayers.) Allah then revealed: And Allah would never make your faith (prayers) to be lost (i.e. the prayers of those Muslims were valid).' " (2:143).

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