All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
The change of the direction of the Qiblah from Jerusalem to the Ka'bah (Makkah) was in the middle of the month of Rajab in the second year after Hijrah (Emigration), according to the most correct opinion. This is the view of the majority of the scholars and the view of Ibn Hajar . The reason for the change of the direction of Qiblah is that the Prophet, sallallaahu ‘alayhi wa sallam, used to perform the prayer to the direction of Jerusalem but he wished to perform the prayer towards the Qiblah of Ibraaheem (Abraham) . Al-Baraa’ narrated, "When the Prophet came to Madeenah, he performed the prayer towards Jerusalem for a period of sixteen or seventeen months but he wished to turn towards the Ka’bah, so Allaah revealed (what means): {Verily, we have seen the turning of your (Mohammad’s ) face towards the heaven. Surely, We shall turn you to a Qiblah (prayer direction) that shall please you.} [Quran, 2:144]." [Al-Bukhari] Thereupon, he was instructed to turn towards the Ka’bah when performing the prayer.
Allaah Knows best.