Accidentally Prayed in The Wrong Direction
Question

One of my neighbours told me that the Qiblah towards which I was praying was wrong and was not in accordance with the Qiblah of the neighbouring mosque. 
Based on this, I changed the direction for a few months, including last Ramadan. Then I found out that the first Qiblah had been correct. What is the ruling on prayers that I did facing the incorrect Qiblah? 
I hope you can answer me because I am confused. Thank you very much.
Praise be to Allah.Facing the Qiblah in prayer: Essential?
Facing the Qiblah is one of the conditions of prayer being valid . Every worshipper has to make sure he faces the Qiblah in his prayer, and strive to make sure he is facing in the right direction, whether by means of natural signs or using a compass , if he is able to do that, or by asking trustworthy people in that place who know in which direction the Qiblah is. 
What happens if you pray in the wrong direction?
It seems most likely in the case that you describe that the deviation from the Qiblah was very slight. This slight deviation is something that can happen when a person who lives in that area is a little confused and people do not pay attention to this slight difference in direction . 
If that is the case, i.e., if the deviation from the Qiblah was slight, then it does not matter and it does not invalidate the prayer, because those who are far away from the Ka`bah have to face in its general direction; they do not have to face precisely in that direction. 
At-Tirmidhi (342) and Ibn Majah (1011) narrated that Abu Hurayrah (may Allah be pleased with him) said: The Messenger of Allah (peace and blessings of Allah be upon him) said: “Whatever is between the east and the west is the Qiblah.” (Classed as authentic by Al-Albani in Al-Irwa’) 
As-San`ani (may Allah have mercy on him) said in Subul As-Salam (1/260). This hadith indicates that what is required is to face in that direction, and not towards the precise location of the Ka`bah when one cannot see it. 
Further evidence to this effect is to be seen in the report narrated by Al-Bukhari (144) and Muslim (264) from Abu Ayyub (may Allah be pleased with him) who said that the Prophet (peace and blessings of Allah be upon him) said: “If you need to relieve yourself, then do not face the Qiblah or turn your back towards it, whether you are urinating or defecating, rather face to the east or to the west.” 
Shaykh Al-Islam (may Allah have mercy on him) said in Sharh Al-`Umdah: 
“This is stating that facing any direction other than the east or the west means that one is either facing the Qiblah or turning one’s back towards it. This was addressed to the people of Madinah and everyone who was in the same direction (in relation to Makkah), because this was the consensus of the Companions (may Allah be pleased with them). `
Umar said: Whatever is between the east and the west is the Qiblah except at the House.  And it was narrated that `Uthman (may Allah be pleased with him) said: How can a man make a mistake in his prayer when whatever is between the east and the west is the Qiblah, so long he does not turn towards the east deliberately. 
Shaykh Ibn `Uthaymin (may Allah have mercy on him) said: “Thus we know that the matter is broad in scope. So if we see a person praying in a direction that is slightly off from the Qiblah, that does not matter, because he is facing in its general direction, and this is what he is obliged to do.” (Ash-Sharh Al-Mumti`, 2/273)
But if the deviation from the direction of the Qiblah was very great , so that you were not praying in the direction of the Qiblah at all, such as if you were praying towards the east when the Qiblah is towards the west or the north, for example, so long as you based your action on the words of someone who you know attaches great importance to the prayer and you thought that they knew better about the direction of the Qiblah, then you do not have to do anything, and the prayers which you offered are still valid, even if you faced the wrong direction when you prayed, because if a person tries his best to get it right, he has done what is required of him, as Allah says (interpretation of the meaning): 
“So keep your duty to Allah and fear Him as much as you can.” [At-Taghabun 64:16]
It says in Fatawa Al-Lajnah Ad-Da’imah (6/314): “If a worshipper does his best to figure out the direction of the Qiblah and prays, then he finds out that he was mistaken, his prayer is still valid.” 
In Fatawa Ash-Shaykh Ibn Baz (may Allah have mercy on him) (10/421) it says: 
“If a believer does his best to figure out the direction of the Qiblah, when he is in the desert or is in a city in which he is not sure where the Qiblah is, and he prays on that basis, then he finds out that he prayed in a direction other than the Qiblah, then he should continue to pray according to his latest estimate, if he believes that it is more correct than his first estimate. His first prayer is still valid because he did his best to work out the Qiblah.”
And Allah knows best.