[Machine] The Messenger of Allah ﷺ arrived in Medina and prayed towards the direction of Bayt al-Maqdis (Jerusalem) for sixteen or seventeen months. The Prophet ﷺ preferred to face the Kaaba, so Allah revealed the verse: "We have certainly seen the turning of your face, [O Muhammad], toward the heaven, and We will surely turn you to a qiblah with which you will be pleased. So turn your face toward al-Masjid al-Haram..." (Quran 2:144)
So, a man who was praying with the Prophet ﷺ among a group of Ansar (Medinan Muslims) who were praying in the direction of Bayt al-Maqdis, said: "I testify that the Messenger of Allah ﷺ has been directed towards the Kaaba." So they turned towards the Kaaba. The foolish among the people, who were the Jews, said: "What has turned them away from their qiblah which they used to face?" (Quran 2:142)
Allah then said: "The east and the west belong to Allah. He guides whom He wills to a straight path." (Quran 2:142)
قَدِمَ رَسُولُ اللهِ ﷺ الْمَدِينَةَ فَصَلَّى نَحْوَ بَيْتِ الْمَقْدِسِ سِتَّةَ عَشَرَ شَهْرًا أَوْ سَبْعَةَ عَشَرَ شَهْرًا وَكَانَ النَّبِيُّ ﷺ يُحِبُّ أَنْ يُوَجَّهَ نَحْوَ الْكَعْبَةِ فَأَنْزَلَ اللهُ ﷻ {قَدْ نَرَى تَقَلُّبَ وَجْهِكَ فِي السَّمَاءِ فَلَنُوَلِّيَنَّكَ قِبْلَةً تَرْضَاهَا فَوَلِّ وَجْهَكَ شَطْرَ الْمَسْجِدِ الْحَرَامِ} [البقرة 144] الْآيَةُ فَمَرَّ رَجُلٌ كَانَ يُصَلِّي مَعَ النَّبِيِّ ﷺ عَلَى قَوْمٍ مِنَ الْأَنْصَارِ يُصَلُّونَ وَهُمْ رُكُوعٌ نَحْوَ بَيْتِ الْمَقْدِسِ فَقَالَ أَشْهَدُ أَنَّ رَسُولَ اللهِ ﷺ قَدْ وُجِّهَ إِلَى الْكَعْبَةِ فَتَحَرَّفُوا نَحْوَ الْكَعْبَةِ فَقَالَ السُّفَهَاءُ مِنَ النَّاسِ وَهُمُ الْيَهُودُ {مَا وَلَّاهُمْ عَنْ قِبْلَتِهِمُ الَّتِي كَانُوا عَلَيْهَا} [البقرة 142] قَالَ اللهُ ﷻ {قُلْ لِلَّهِ الْمَشْرِقُ وَالْمَغْرِبُ يَهْدِي مَنْ يَشَاءُ إِلَى صِرَاطٍ مُسْتَقِيمٍ} [البقرة 142]