Messenger of Allah ﷺ prayed facing Baitul-Maqdis for sixteen or seventeen months but he loved to face the Kaʿba (at Mecca) so Allah revealed: "Verily, We have seen the turning of your face to the heaven!" (2:144) So the Prophet ﷺ faced the Kaʿba and the fools amongst the people namely "the Jews" said, "What has turned them from their Qibla (Baitul-Maqdis) which they formerly observed"" (Allah revealed): "Say: 'To Allah belongs the East and the West. He guides whom he will to a straight path'." (2:142) A man prayed with the Prophet (facing the Kaʿba) and went out. He saw some of the Ansar praying the ʿAsr prayer with their faces towards Baitul-Maqdis, he said, "I bear witness that I prayed with Messenger of Allah ﷺ facing the Kaʿba." So all the people turned their faces towards the Kaʿba.
كَانَ رَسُولُ اللَّهِ ﷺ صَلَّى نَحْوَ بَيْتِ الْمَقْدِسِ سِتَّةَ عَشَرَ أَوْ سَبْعَةَ عَشَرَ شَهْرًا وَكَانَ رَسُولُ اللَّهِ ﷺ يُحِبُّ أَنْ يُوَجَّهَ إِلَى الْكَعْبَةِ فَأَنْزَلَ اللَّهُ {قَدْ نَرَى تَقَلُّبَ وَجْهِكَ فِي السَّمَاءِ} فَتَوَجَّهَ نَحْوَ الْكَعْبَةِ وَقَالَ السُّفَهَاءُ مِنَ النَّاسِ وَهُمُ الْيَهُودُ مَا وَلاَّهُمْ عَنْ قِبْلَتِهِمُ الَّتِي كَانُوا عَلَيْهَا {قُلْ لِلَّهِ الْمَشْرِقُ وَالْمَغْرِبُ يَهْدِي مَنْ يَشَاءُ إِلَى صِرَاطٍ مُسْتَقِيمٍ} فَصَلَّى مَعَ النَّبِيِّ ﷺ رَجُلٌ ثُمَّ خَرَجَ بَعْدَ مَا صَلَّى فَمَرَّ عَلَى قَوْمٍ مِنَ الأَنْصَارِ فِي صَلاَةِ الْعَصْرِ نَحْوَ بَيْتِ الْمَقْدِسِ فَقَالَ هُوَ يَشْهَدُ أَنَّهُ صَلَّى مَعَ رَسُولِ اللَّهِ ﷺ وَأَنَّهُ تَوَجَّهَ نَحْوَ الْكَعْبَةِ فَتَحَرَّفَ الْقَوْمُ حَتَّى تَوَجَّهُوا نَحْوَ الْكَعْبَةِ