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 Messenger of Allah ﷺ 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).
أَنَّ النَّبِيَّ ﷺ كَانَ أَوَّلَ مَا قَدِمَ الْمَدِينَةَ نَزَلَ عَلَى أَجْدَادِهِ أَوْ قَالَ أَخْوَالِهِ مِنَ الأَنْصَارِ وَأَنَّهُ صَلَّى قِبَلَ بَيْتِ الْمَقْدِسِ سِتَّةَ عَشَرَ شَهْرًا أَوْ سَبْعَةَ عَشَرَ شَهْرًا وَكَانَ يُعْجِبُهُ أَنْ تَكُونَ قِبْلَتُهُ قِبَلَ الْبَيْتِ وَأَنَّهُ صَلَّى أَوَّلَ صَلاَةٍ صَلاَّهَا صَلاَةَ الْعَصْرِ وَصَلَّى مَعَهُ قَوْمٌ فَخَرَجَ رَجُلٌ مِمَّنْ صَلَّى مَعَهُ فَمَرَّ عَلَى أَهْلِ مَسْجِدٍ وَهُمْ رَاكِعُونَ فَقَالَ أَشْهَدُ بِاللَّهِ لَقَدْ صَلَّيْتُ مَعَ رَسُولِ اللَّهِ ﷺ قِبَلَ مَكَّةَ فَدَارُوا كَمَا هُمْ قِبَلَ الْبَيْتِ وَكَانَتِ الْيَهُودُ قَدْ أَعْجَبَهُمْ إِذْ كَانَ يُصَلِّي قِبَلَ بَيْتِ الْمَقْدِسِ وَأَهْلُ الْكِتَابِ فَلَمَّا وَلَّى وَجْهَهُ قِبَلَ الْبَيْتِ أَنْكَرُوا ذَلِكَ قَالَ زُهَيْرٌ حَدَّثَنَا أَبُو إِسْحَاقَ عَنِ الْبَرَاءِ فِي حَدِيثِهِ هَذَا أَنَّهُ مَاتَ عَلَى الْقِبْلَةِ قَبْلَ أَنْ تُحَوَّلَ رِجَالٌ وَقُتِلُوا فَلَمْ نَدْرِ مَا نَقُولُ فِيهِمْ فَأَنْزَلَ اللَّهُ تَعَالَى {وَمَا كَانَ اللَّهُ لِيُضِيعَ إِيمَانَكُمْ}