It is stated in the books of seerah (the Prophet’s biography) that the Prophet (peace and blessings be upon him) and the Muslims were facing Jerusalem while praying, i.e., they regarded it as the qiblah.
this was done for a year and four months after migrating to Madinah. It was the desire of the Prophet (peace and blessings be upon him) that the Ka`bah be made the qiblah for Prayers. The reason for this was that the Ka`bah (built by Prophet Ibrahim and Isma`il, peace and blessings be upon them) was held by all as the holiest of sites, incomparable in sanctity to any other sanctum or shrine. It was a great test for the earlier Muslims by being asked to face Jerusalem instead of the Ka`bah, but they withstood this trial by dutifully obeying the Divine command.
In demonstrating their devotion to the Prophet (peace and blessings be upon him) they would always reply, regardless of any reservation they had against any particular ordinance: (We hear, and we obey) (An-Nur 24: 51) and (We believe therein: The whole is from our Lord.) (Aal `Imran 3: 7)
thus, after passing this test, with flying color, the qiblah for the Prayer was changed to the Ka`bah.
In this context, the Glorious Qur’an says, (Thus We have appointed you a middle nation, that ye may be witness against mankind and that the Messenger may be witness against you. And We appointed the qiblah which you formerly observed only that We might know him who followeth the Messenger, from him who turneth on his heels. In truth it was a hard (test) save for those whom Allah guided.) (Al-Baqarah 2: 143). In compliance with divine command, the Muslims changed their direction promptly in Prayer towards the Ka`bah, which was then selected as the qiblah for all the believers living in any part of the world for all times to come.
Also on this issue, Al-Bara’ ibn `Azib narrated that Allah’s Messenger (peace and blessings be upon him) prayed facing Bait ul-Maqdis (Jerusalem) for 16 or 17 months, but due to his vehement desire to face the Ka`bah, Allah revealed: (Verily, We have seen the turning of your face to the heaven!) (Al-Baqarah 2: 144)
the moment the Prophet (peace and blessings be upon him) began facing the Ka`bah the Jews started exclaiming, (What has turned them from their qiblah (Bait ul-Maqdis) which they formerly observed?” Allah revealed in reply: “Say: ‘To Allah belongs the East and the West. He guides whom he will to a straight path.) (Al-Baqarah 2: 142)
It happened that a man, after praying with the Prophet, facing the Ka`bah, saw some of the Ansar praying the `Asr Prayer facing Bait ul-Maqdis. Thereupon, he said: “I bear witness that I prayed with Allah’s Messenger (peace and blessings be upon him) facing the Ka`bah.” So on the spot all the people turned their faces towards the Ka`bah. (Reported by Al-Bukhari)