The wisdom behind this change of direction was, in fact, a trial and test from Allah, as He made clear in His words: “We did not appoint the direction you formerly faced except to know those who follow the Messenger from those who turn back on their heels. It was indeed a tremendous test—except for those whom Allah has guided. And Allah would never allow your faith to be lost; truly Allah is Gentle, Merciful toward mankind.” (al-Baqarah 2:143)
(b) As for the Sunnah, it is established by the Prophet’s ﷺ instruction to the man who performed his prayer incorrectly:
“When you stand for prayer, perfect your wudu (ritual ablution), then face the qiblah (direction of prayer) and pronounce the takbir…”—and so on to the end of the report.
This is a portion of the famous ḥadith of “the man who prayed badly,” narrated by Abu Hurayrah (may Allah be pleased with him):
A man entered the mosque and prayed while the Messenger of Allah ﷺ was sitting in one corner. The man then came and greeted him. The Prophet replied—one version adds, “And upon you be peace”—and said, “Go back and pray, for you have not prayed.” The man returned and prayed, then came and greeted him again. The Prophet said, “And upon you be peace; go back and pray, for you have not prayed.” On the third occasion the man said, “Teach me, then.” The Prophet ﷺ said:
“When you rise for prayer, perfect your wudu, then face the qiblah and pronounce the takbir. Recite whatever is easy for you from the Quran, then bow until you are at ease in bowing. Raise your head until you stand straight, then prostrate until you are at ease in prostration. Rise until you are sitting upright and at ease, then prostrate again until you are at ease. Finally, rise until you are standing straight. Do this throughout your entire prayer.”