When the Messenger of Allah ﷺ arrived in Medina, he prayed facing Bayt al-Maqdis for sixteen or seventeen months, though he longed to face the Kaabah. Then Allah 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" [Baqarah 2:144]. So he turned toward the Kaabah.A man had prayed the afternoon prayer with him, then went out and passed by some people from the Ansar. He told them that he bore witness that he had prayed with the Prophet ﷺ and that the qiblah had been turned toward the Kaabah. So they shifted their direction while they were bowing in the afternoon prayer.
حدثنا يحيى حدثنا وكيع عن إسرائيل عن أبي إسحاق
لما قدم رسول الله ﷺ المدينة صلى نحو بيت المقدس ستة عشر أو سبعة عشر شهرا وكان يحب أن يوجه إلى الكعبة فأنزل الله تعالى {قد نرى تقلب وجهك في السماء فلنولينك قبلة ترضاها} فوجه نحو الكعبة وصلى معه رجل العصر ثم خرج فمر على قوم من الأنصار فقال هو يشهد أنه صلى مع النبي ﷺ وأنه قد وجه إلى الكعبة فانحرفوا وهم ركوع في صلاة العصر