The Messenger of Allah ﷺ prayed facing Bayt al-Maqdis for sixteen, or seventeen, months. The Messenger of Allah ﷺ wished to be turned toward the Kaaba, so Allah revealed, "We have surely seen you turning your face about toward the heaven" [Baqarah 2:144], and he turned toward the Kaaba.The foolish among the people, meaning the Jews, said, "What has turned them away from the qiblah they used to face? Say, To Allah belong the east and the west. He guides whom He wills to a straight path" [Baqarah 2:142].A man prayed with the Prophet ﷺ, and after finishing he went out and passed by a group of the Ansar who were praying Asr facing Bayt al-Maqdis. He said, "I bear witness that I prayed with the Messenger of Allah ﷺ, and that he turned toward the Kaaba."So the group shifted around until they were facing the Kaaba.
كان رسول الله ﷺ صلى نحو بيت المقدس ستة عشر- أو سبعة عشر- شهرا. وكان رسول الله ﷺ يحب أن يوجه إلى الكعبة، فأنزل الله: {قد نرى تقلب وجهك في السماء} [ سورة البقرة: ١٤٤] فتوجه نحو الكعبة. وقال السفهاء من الناس-وهم اليهود-: {ما ولهم عن قبلتهم التي كانوا عليها قل لله المشرق والمغرب يهدى من يشاء إلى صراط مستقيم} [ سورة البقرة: ١٤٢]. فصلى مع النبي ﷺ رجل، ثم خرج بعد ما صلى فمر على قوم من الأنصار في صلاة العصر نحو بيت المقدس، فقال: هو يشهد أنه صلى مع رسول الله ﷺ، وأنه توجه نحو الكعبة. فتحرف القوم حتى توجهوا نحو الكعبة.
Bukhari narrated it in the Book of Prayer (399), and Muslim in the Book of Mosques (525), both from the hadith of Abu Ishaq, from Bara bin Azib, and the wording given here is Bukhari's, while Muslim mentioned it in a shortened form similar to it. Bukhari alone mentioned in the Book of Faith (40), "While they were bowing, they turned around as they were, facing the House," and the Jews had been pleased when he used to pray facing Bayt al-Maqdis, along with the People of the Book, but when he turned his face toward the House, they disapproved of that.