The Prophet ﷺ prayed facing Bait-ulMaqdis (i.e. Jerusalem) for sixteen or seventeen months but he wished that his Qibla would be the Kaʿba (at Mecca). (So Allah Revealed (2.144) and he offered ʿAsr prayers(in his Mosque facing Kaʿba at Mecca) and some people prayed with him. A man from among those who had prayed with him, went out and passed by some people offering prayer in another mosque, and they were in the state of bowing. He said, "I, (swearing by Allah,) testify that I have prayed with the Prophet ﷺ facing Mecca." Hearing that, they turned their faces to the Kaʿba while they were still bowing. Some men had died before the Qibla was changed towards the Kaʿba. They had been killed and we did not know what to say about them (i.e. whether their prayers towards Jerusalem were accepted or not). So Allah revealed:-- "And Allah would never make your faith (i.e. prayer) to be lost (i.e. your prayers offered (towards Jerusalem). Truly Allah is Full of Pity, Most Merciful towards mankind." (2.143)
أَنَّ رَسُولَ اللَّهِ ﷺ صَلَّى إِلَى بَيْتِ الْمَقْدِسِ سِتَّةَ عَشَرَ شَهْرًا أَوْ سَبْعَةَ عَشَرَ شَهْرًا وَكَانَ يُعْجِبُهُ أَنْ تَكُونَ قِبْلَتُهُ قِبَلَ الْبَيْتِ وَإِنَّهُ صَلَّى أَوْ صَلاَّهَا صَلاَةَ الْعَصْرِ وَصَلَّى مَعَهُ قَوْمٌ فَخَرَجَ رَجُلٌ مِمَّنْ كَانَ صَلَّى مَعَهُ فَمَرَّ عَلَى أَهْلِ الْمَسْجِدِ وَهُمْ رَاكِعُونَ قَالَ أَشْهَدُ بِاللَّهِ لَقَدْ صَلَّيْتُ مَعَ النَّبِيِّ ﷺ قِبَلَ مَكَّةَ فَدَارُوا كَمَا هُمْ قِبَلَ الْبَيْتِ وَكَانَ الَّذِي مَاتَ عَلَى الْقِبْلَةِ قَبْلَ أَنْ تُحَوَّلَ قِبَلَ الْبَيْتِ رِجَالٌ قُتِلُوا لَمْ نَدْرِ مَا نَقُولُ فِيهِمْ فَأَنْزَلَ اللَّهُ {وَمَا كَانَ اللَّهُ لِيُضِيعَ إِيمَانَكُمْ إِنَّ اللَّهَ بِالنَّاسِ لَرَءُوفٌ رَحِيمٌ }