Imam Al-Bukhari reported that Al-Bara' bin `Azib narrated: "Allah's Messenger offered his prayers facing Bayt Al-Maqdis (Jerusalem) for sixteen or seventeen months, but he wished that he could pray facing the Ka`bah (at Makkah). The first prayer which he offered (facing the Ka`bah) was the `Asr (Afternoon) prayer in the company of some people. Then one of those who had offered that prayer with him, went out and passed by some people in a mosque who were in the bowing position (in Ruku`) during their prayers (facing Jerusalem). He addressed them saying, `By Allah, I bear witness that I have offered prayer with the Prophet facing Makkah (Ka`bah).' Hearing that, those people immediately changed their direction towards the House (Ka`bah) while still as they were (i.e., in the same bowing position). Some Muslims who offered prayer towards the previous Qiblah (Jerusalem) before it was changed towards the House (the Ka`bah in Makkah) had died or had been martyred, and we did not know what to say about them (regarding their prayers towards Jerusalem). Allah then revealed:
﴿ وَمَا كَانَ ٱللَّهُ لِيُضِيعَ إِيمَـٰنَكُمۡۚ إِنَّ ٱللَّهَ بِٱلنَّاسِ لَرَءُوفٌ۬ رَّحِيمٌ۬ ﴾
(And Allah would never make your faith (prayers) to be lost (i.e., the prayers of those Muslims were valid)) (2:143).''
Al-Bukhari collected this narration, while Muslim collected it using another chain of narrators. Muhammad bin Ishaq reported that Al-Bara' narrated: Allah's Messenger used to offer prayers towards Bayt Al-Maqdis (in Jerusalem), but would keep looking at the sky awaiting Allah's command (to change the Qiblah). Then Allah revealed:
﴿ قَدۡ نَرَىٰ تَقَلُّبَ وَجۡهِكَ فِى ٱلسَّمَآءِۖ فَلَنُوَلِّيَنَّكَ قِبۡلَةً۬ تَرۡضَٮٰهَاۚ فَوَلِّ وَجۡهَكَ شَطۡرَ ٱلۡمَسۡجِدِ ٱلۡحَرَامِۚ ﴾
(Verily, We have seen the turning of your (Muhammad's) face towards the heaven. Surely, We shall turn you to a Qiblah (prayer direction) that shall please you, so turn your face in the direction of Al-Masjid Al-Haram (at Makkah).) (2:144)
A man from among the Muslims then said, "We wish we could know about those among us who died before the Qiblah was changed (i.e., towards Makkah) and also about our own prayers, that we had performed towards Bayt Al-Maqdis.'' Allah then revealed:
﴿ وَمَا كَانَ ٱللَّهُ لِيُضِيعَ إِيمَـٰنَكُمۡۚ ﴾
(And Allah would never make your faith (prayers) to be lost.) (2:143)
The fools among the people, meaning the People of the Scripture (Jews and Christians), said, "What made them change the former Qiblah that they used to face'' Allah then revealed:
﴿ سَيَقُولُ ٱلسُّفَهَآءُ مِنَ ٱلنَّاسِ ﴾
(The fools (idolaters, hypocrites, and Jews) among the people will say...)
until the end of the Ayah.
`Ali bin Abu Talhah related that Ibn `Abbas said: When Allah's Messenger migrated to Al-Madinah, Allah commanded him to face Bayt Al-Maqdis (Jerusalem). The Jews were delighted then. Allah's Messenger faced Jerusalem for over ten months. However, he liked (to offer prayer in the direction of) Prophet Ibrahim's Qiblah (the Ka`bah in Makkah) and used to supplicate to Allah and kept looking up to the sky (awaiting Allah's command in this regard). Allah then revealed:
﴿ فَوَلُّواْ وُجُوهَكُمۡ شَطۡرَهُ ۥۗ ﴾
(turn your faces (in prayer) in that direction.) meaning, its direction. The Jews did not like this change and said, "What made them change the Qiblah that they used to face (meaning Jerusalem)'' Allah revealed:
﴿ قُل لِّلَّهِ ٱلۡمَشۡرِقُ وَٱلۡمَغۡرِبُۚ يَہۡدِى مَن يَشَآءُ إِلَىٰ صِرَٲطٍ۬ مُّسۡتَقِيمٍ۬ ﴾
(Say (O Muhammad): "To Allah belong both, east and the west. He guides whom He wills to the straight way.'')
There are several other Ahadith on this subject. In summary, Allah's Messenger was commanded to face Bayt Al-Maqdis (during the prayer) and he used to offer prayer towards it in Makkah between the two corners (of Ka`bah), so that the Ka`bah would be between him and Bayt Al-Maqdis. When the Prophet migrated to Al-Madinah, this practice was no longer possible; then Allah commanded him to offer prayer towards Bayt Al-Maqdis, as Ibn Abbas and the majority of the scholars have stated.
Al-Bukhari reported in his Sahih that the news (of the change of Qiblah) was conveyed to some of the Ansar while they were performing the `Asr (Afternoon) prayer towards Bayt Al-Maqdis, upon hearing that, they immediately changed their direction and faced the Ka`bah.
It is reported in the Sahihayn (Al-Bukhari Muslim) that Ibn `Umar narrated: While the people were in Quba' (Mosque) performing the Fajr (Dawn) prayer, a man came and said, "A (part of the) Qur'an was revealed tonight to Allah's Messenger and he was commanded to face the Ka`bah. Therefore, face the Ka`bah. They were facing Ash-Sham, so they turned towards the Ka`bah.
These Hadiths prove that the Nasikh (a Text that abrogates a previous Text) only applies after one acquires knowledge of it, even if the Nasikh had already been revealed and announced. This is why the Companions mentioned above were not commanded to repeat the previous `Asr, Maghrib and `Isha' prayers (although they had prayed them towards Jerusalem after Allah had changed the Qiblah). Allah knows best.
When the change of Qiblah (to Ka`bah in Makkah) occurred, those inflicted with hypocrisy and mistrust, and the disbelieving Jews, both were led astray from the right guidance and fell into confusion. They said:
﴿ مَا وَلَّٮٰهُمۡ عَن قِبۡلَتِہِمُ ٱلَّتِى كَانُواْ عَلَيۡهَاۚ ﴾
(What has turned them (Muslims) from their Qiblah to which they used to face in prayer.)
They asked, "What is the matter with these people (Muslims) who one time face this direction (Jerusalem), and then face that direction (Makkah)'' Allah answered their questions when He stated:
﴿ قُل لِّلَّهِ ٱلۡمَشۡرِقُ وَٱلۡمَغۡرِبُۚ ﴾
(Say (O Muhammad) : "To Allah belong both, east and the west.) meaning, the command, the decision and the authority are for Allah Alone. Hence:
﴿ فَأَيۡنَمَا تُوَلُّواْ فَثَمَّ وَجۡهُ ٱللَّهِۚ ﴾
(...so wherever you turn (yourselves or your faces) there is the Face of Allah (and He is High above, over His Throne).) (2:115),
﴿ لَّيۡسَ ٱلۡبِرَّ أَن تُوَلُّواْ وُجُوهَكُمۡ قِبَلَ ٱلۡمَشۡرِقِ وَٱلۡمَغۡرِبِ وَلَـٰكِنَّ ٱلۡبِرَّ مَنۡ ءَامَنَ بِٱللَّهِ ﴾
( It is not Al-Birr (piety, righteousness) that you turn your faces towards east and (or) west (in prayers); but Al-Birr is the one who believes in Allah.) (2:177) This statement means, the best act is to adhere to Allah's commands. Hence, wherever He commands us to face, we should face. Also, since obedience requires implementing Allah's commands, if He commands us every day to face different places, we are His servants and under His disposal, and we face whatever He orders us to face. Certainly, Allah's care and kindness towards His servant and Messenger, Muhammad, and certainly, his Ummah (Muslim nation) is profoundly great. Allah has guided them to the Qiblah of (Prophet) Ibrahim - Allah's Khalil (intimate friend). He has commanded them to face the Ka`bah, the most honorable house (of worship) on the face of the earth, which was built by Ibrahim Al-Khalil in the Name of Allah, the One without a partner. This is why Allah said afterwards:
﴿ قُل لِّلَّهِ ٱلۡمَشۡرِقُ وَٱلۡمَغۡرِبُۚ يَہۡدِى مَن يَشَآءُ إِلَىٰ صِرَٲطٍ۬ مُّسۡتَقِيمٍ۬ ﴾
(Say (O Muhammad): "To Allah belong both, east and the west. He guides whom He wills to the straight way.'')
Imam Ahmad reported that `A'ishah (the Prophet's wife) said that Allah's Messenger said about the People of the Scripture (Jews and Christians):
« إنَّهُم لا يَحْسِدونَنَا عَلَى شَيْء كَمَا يَحْسِدونَنا عَلَى يَوْمِ الْجُمُعَةِ الَّتِي هَدَانَا اللهُ لَهَا وَضَلُوا عَنْهَا وَعَلَى الْقِبْلَةِ الَّتِي هَدَانَا اللهُ لَهَا وَضَلُّوا عَنْهَا وَعَلَى قَوْلِنَا خَلْفَ الإِمَامِ: آمِين »
(They do not envy us for a matter more than they envy us for Jumu`ah (Friday) to which Allah has guided us and from which they were led astray; for the (true) Qiblah to which Allah has directed us and from which they were led astray; and for our saying `Amin' behind the Imam (leader of the prayer).)