All perfect praise be to Allah, The Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
The intention to perform a certain prayer is a must. The Prophet, sallallaahu ‘alayhi wa sallam, said, “...and every person will get the reward according to what he has intended.” [Al-Bukhari and Muslim] An-Nawawi wrote, “It means that specifying what one intends to do is a condition...”
If you hold the intention of performing a certain prayer - the Maghrib, for instance - then your intention is valid. The thought of ‘Ishaa' that crossed you mind while you intend to pray the Maghrib is of no effect. Even if you said the word ‘Isha' while intending to pray the Maghrib, then it is your intention that counts and not what you said. The Prophet, sallallaahu ‘alayhi wa sallam, said, “Verily, the deeds are judged by the intentions.”
It is possible that these thoughts are whispers from Satan to distract you from the prayer. As long as you did not reject the intention of performing the Maghrib after having held it or there was no long interval (according to common practice) between holding the intention and making the opening Takbeer, then your prayer is valid, Allah willing.
Ibn Qudaamah wrote, “It is permissible to hold the intention before reciting the opening Takbeer with a short period of time. If the interval is long or the person cancels his intention, it is not valid and insufficient in terms of clearing himself of his liability from the obligation.” [Al-Mughni]
It should be noted that the place of the intention is the heart. It is enough to have the intention in your heart without pronouncing it with your tongue. You should know that holding the intention does not require a significant effort. Rather, it is simply defined as what the person intends to do and the knowledge of it. Shaykhul-Islam Ibn Taymiyyah wrote, “The intention is consequent to knowledge. When one knows what he wants to do, he necessarily intends it. Whenever the person knows the act of worship that he is performing, he has essentially held the intention to perform it. It is inconceivable that one would intend to do something while not knowing it.” [Majmoo’ Al-Fataawa]
Allah knows best.