When the Messenger of Allah ﷺ arrived in Medina, he prayed facing Bayt al-Maqdis for sixteen or seventeen months, though the Messenger of Allah ﷺ longed to face the Kaabah. So Allah Most High revealed, "Indeed, We see the turning of your face toward the heaven, so We will surely turn you to a qiblah with which you will be pleased. So turn your face toward the Sacred Mosque" [Baqarah 2:144]. He then turned toward the Kaabah, which is what he loved.A man had prayed the Asr prayer with him, then passed by some people from the Ansar who were bowing in the Asr prayer facing Bayt al-Maqdis. He said that he bore witness that he had prayed with the Messenger of Allah ﷺ and that he had turned to face the Kaabah. So they shifted their direction while still bowing.
حدثنا هناد حدثنا وكيع عن إسرائيل عن أبي إسحق
لما قدم رسول الله ﷺ المدينة صلى نحو بيت المقدس ستة أو سبعة عشر شهرا وكان رسول الله ﷺ يحب أن يوجه إلى الكعبة فأنزل الله تعالى {قد نرى تقلب وجهك في السماء فلنولينك قبلة ترضاها فول وجهك شطر المسجد الحرام} فوجه نحو الكعبة وكان يحب ذلك فصلى رجل معه العصر ثم مر على قوم من الأنصار وهم ركوع في صلاة العصر نحو بيت المقدس فقال هو يشهد أنه صلى مع رسول الله ﷺ وأنه قد وجه إلى الكعبة قال فانحرفوا وهم ركوع
Agreed upon. Bukhari narrated it in the Book of Faith, Chapter "Prayer is Part of Faith," no. 40, and Muslim, no. 525, from the hadith of Abu Ishaq Sabi, through this same chain.On this topic, there are also narrations from Ibn Umar [coming up: 341], Ibn Abbas [coming up: 2964], Umarah bin Aws [Abu Naeem in Marifat Sahabah, 4/207, no. 5229], Amr bin Awf Muzani [Bukhari in Tarikh al-Kabir, 6/307], and Anas [Muslim, no. 527].