During the early days of Islam, Prophet Muhammad PBUH faced the Bayt al-Maqdis in Jerusalem during his prayers, just like the earlier teachings of the People of the Book (Jews and Christians).
However, the Prophet possessed a higher spiritual attachment to the Kaaba, as the Kaaba was built by his great-grandfather, Abraham, to emphasize monotheism.
So when in Mecca, the Prophet offered his prayers before the Maqam Ibrahim, because it is aligned to both the Kaaba and Bayt al-Maqdis.
After the migration to Medina, the Prophet felt disheartened that he could no longer do the same thing, since the Kaaba is in the opposite direction.
Out of politeness & refined manner, the Prophet did not supplicate for the change of direction, he just raised his head toward the sky before praying, but isn’t Allah The All-Knowing?
So Allah revealed,
“We have certainly seen the turning of your face, (O Muhammad), toward the heaven, and We will surely turn you to a Qiblah with which you will be pleased. So turn your face toward al-Masjid al-Haram. And wherever you (believers) are, turn your faces toward it (in prayer).” (Quran 2:144)
The Jews were in anger for this, as Allah said,
“The foolish among the people will say, ‘What has turned them away from their Qibla, which they used to face?” (Quran 2:142)
Allah answered them,
“Say, “To Allah belongs the east and the west.” (Quran 2:142)
And here’s the reason for the change, Allah said,
“And We did not make the Qibla which you used to face except that We might make evident who would follow the Messenger from who would turn back on his heels.” (Quran 2:143)
The change was on the 15th of Shaaban, the 2nd year after Hijra in the middle of congregational prayer during in Medina.