Narrated Bara' bin `Azib
Allah's Messenger (ﷺ) prayed facing Baitul-Maqdis for sixteen or seventeen months but he loved to face the Ka`ba (at Mecca) so Allah revealed: "Verily, We have seen the turning of your face to the heaven!" (2:144) So the Prophet (ﷺ) faced the Ka`ba and the fools amongst the people namely "the Jews" said, "What has turned them from their Qibla (Baitul-Maqdis) which they formerly observed"" (Allah revealed): "Say: 'To Allah belongs the East and the West. He guides whom he will to a straight path'." (2:142) A man prayed with the Prophet (facing the Ka`ba) and went out. He saw some of the Ansar praying the `Asr prayer with their faces towards Baitul-Maqdis, he said, "I bear witness that I prayed with Allah's Messenger (ﷺ) facing the Ka`ba." So all the people turned their faces towards the Ka`ba.