When the Messenger of Allah ﷺ first arrived in Medina, he stayed with his maternal relatives among the Ansar. He prayed facing Bayt al-Maqdis for sixteen or seventeen months, but he wished that his prayer direction would be toward the Kabah.The first prayer he prayed facing the Kabah was the afternoon prayer, and a group of people prayed it with him. One of those who had prayed with him went out and passed by the people of a mosque while they were bowing in prayer. He said, "I swear by Allah, I have just prayed with the Messenger of Allah ﷺ facing Mecca." So they turned, just as they were, to face the Kabah.He was pleased to have the prayer direction changed to the Kabah. The Jews and the People of the Book had been pleased when he prayed facing Bayt al-Maqdis, but when he turned his face toward the Kabah, they rejected this.
حدثنا حسن بن موسى حدثنا زهير حدثنا أبو إسحاق
أن رسول الله ﷺ كان أول ما قدم المدينة نزل على أجداده وأخواله من الأنصار وأنه صلى قبل بيت المقدس ستة عشر أو سبعة عشر شهرا وكان يعجبه أن تكون قبلته قبل البيت وأنه صلى أول صلاة صلاها صلاة العصر وصلى معه قوم فخرج رجل ممن صلى معه فمر على أهل مسجد وهم راكعون فقال أشهد بالله لقد صليت مع رسول الله ﷺ قبل مكة قال فداروا كما هم قبل البيت وكان يعجبه أن يحول قبل البيت وكان اليهود قد أعجبهم إذ كان يصلي قبل بيت المقدس وأهل الكتاب فلما ولى وجهه قبل البيت أنكروا ذلك
Its isnad is sahih. Muslim narrated it, 1/375, no. 526, in the Book of Mosques, chapter on the changing of the qibla. Tirmidhi also narrated it, 2/169, no. 340, and said, hasan sahih. Nasai narrated it in the chapter on the qibla, 2/60, no. 742, and Ibn Majah, 1/322, no. 1010.