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) (Using translation from Bukhārī 4486)
أَنَّ رَسُولَ اللهِ ﷺ صَلَّى قِبَلَ بَيْتِ الْمَقْدِسِ سِتَّةَ عَشَرَ شَهْرًا أَوْ سَبْعَةَ عَشَرَ شَهْرًا وَكَانَ يُعْجِبُهُ أَنْ تَكُونَ قِبْلَتُهُ قِبَلَ الْبَيْتِ وَأَنَّهُ صَلَّى صَلَاةَ الْعَصْرِ وَصَلَّى مَعَهُ قَوْمٌ فَخَرَجَ رَجُلٌ مِمَّنْ كَانَ صَلَّى مَعَهُ فَمَرَّ عَلَى أَهْلِ مَسْجِدٍ وَهُمْ رَاكِعُونَ فَقَالَ أَشْهَدُ بِاللهِ لَقَدْ صَلَّيْتُ مَعَ رَسُولِ اللهِ ﷺ قِبَلَ مَكَّةَ فَدَارُوا كَمَا هُمْ قِبَلَ الْبَيْتِ