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.
When one prays towards a direction other than the Qiblah after exerting his best effort to determine it and then knows that it was the wrong direction, he is required to make up for the past prayers, according to the opinion of Shaafi‘i scholars. Acting according to this opinion is more cautious and more likely to absolve one from liability. The majority of scholars, however, are of the opinion that he is not required to make up for these prayers. According to the first opinion, one is required to make up for the past prayers in sequence.
This is the ruling when one exerts his best to determine the direction of the Qiblah and then finds out that he was mistaken. But, when he prays while neither exerting his best nor imitating someone, he is required to make up for the prayers even if he accidentally prayed towards the right direction. This is the opinion of the Shaafi‘i and Hanbali scholars and it is the preponderant opinion, as such a person prayed in a state of doubt.