Imam Bukhari narrates: Abdullah bin Raja narrated to us, he said: Israil narrated to us, from Abu Ishaq from Hazrat Al-Bara bin Azib, Allah be pleased with him, he narrates that
the Messenger of Allah ﷺ prayed facing towards Bait al-Maqdis for sixteen or seventeen months, and the Messenger of Allah ﷺ liked that he be turned towards the Ka'bah, then Allah, Mighty and Majestic, revealed this verse: We see the turning of your face again and again towards the sky. (Al-Baqarah: ) Then he was turned towards the Ka'bah, and among the people, the ignorant ones said, and they were the Jews: What has turned them away from their qiblah, upon which they were before? Say: The East and the West belong to Allah alone. He guides whom He wills towards the straight path. (Al-Baqarah: ) Then a person prayed along with the Prophet ﷺ, he came out after praying and passed by the people of the Ansar who were praying the Asr prayer facing towards Bait al-Maqdis, so he said: He bears witness that he has (just now) prayed along with the Messenger of Allah ﷺ and he was facing towards the Ka'bah, then those people turned and they faced towards the Ka'bah.
حَدَّثَنَا عَبْدُ اللَّهِ بْنُ رَجَاءٍ، قَالَ حَدَّثَنَا إِسْرَائِيلُ، عَنْ أَبِي إِسْحَاقَ، عَنِ الْبَرَاءِ بْنِ عَازِبٍ ـ رضى الله عنهما ـ قَالَ كَانَ رَسُولُ اللَّهِ صلى الله عليه وسلم صَلَّى نَحْوَ بَيْتِ الْمَقْدِسِ سِتَّةَ عَشَرَ أَوْ سَبْعَةَ عَشَرَ شَهْرًا، وَكَانَ رَسُولُ اللَّهِ صلى الله عليه وسلم يُحِبُّ أَنْ يُوَجَّهَ إِلَى الْكَعْبَةِ، فَأَنْزَلَ اللَّهُ {قَدْ نَرَى تَقَلُّبَ وَجْهِكَ فِي السَّمَاءِ} فَتَوَجَّهَ نَحْوَ الْكَعْبَةِ، وَقَالَ السُّفَهَاءُ مِنَ النَّاسِ ـ وَهُمُ الْيَهُودُ ـ مَا وَلاَّهُمْ عَنْ قِبْلَتِهِمُ الَّتِي كَانُوا عَلَيْهَا {قُلْ لِلَّهِ الْمَشْرِقُ وَالْمَغْرِبُ يَهْدِي مَنْ يَشَاءُ إِلَى صِرَاطٍ مُسْتَقِيمٍ} فَصَلَّى مَعَ النَّبِيِّ صلى الله عليه وسلم رَجُلٌ ثُمَّ خَرَجَ بَعْدَ مَا صَلَّى، فَمَرَّ عَلَى قَوْمٍ مِنَ الأَنْصَارِ فِي صَلاَةِ الْعَصْرِ نَحْوَ بَيْتِ الْمَقْدِسِ فَقَالَ هُوَ يَشْهَدُ أَنَّهُ صَلَّى مَعَ رَسُولِ اللَّهِ صلى الله عليه وسلم، وَأَنَّهُ تَوَجَّهَ نَحْوَ الْكَعْبَةِ. فَتَحَرَّفَ الْقَوْمُ حَتَّى تَوَجَّهُوا نَحْوَ الْكَعْبَةِ.
Reference: Sahih Al Bukhari 399