When the Messenger of Allah ﷺ arrived in Medina, he prayed facing Bayt al-Maqdis for sixteen months, then he was directed toward the Kaabah. A man who had prayed with the Prophet ﷺ passed by a group of the Ansar and said, "I bear witness that the Messenger of Allah ﷺ has been directed toward the Kaabah," so they turned to face the Kaabah.
أخبرنا محمد بن إسمعيل بن إبراهيم قال حدثنا إسحق بن يوسف الأزرق عن زكريا بن أبي زائدة عن أبي إسحق
قدم رسول الله ﷺ المدينة فصلى نحو بيت المقدس ستة عشر شهرا ثم وجه إلى الكعبة فمر رجل قد كان صلى مع النبي ﷺ على قوم من الأنصار فقال أشهد أن رسول الله ﷺ قد وجه إلى الكعبة فانحرفوا إلى الكعبة