Praise be to Allah.
If a Muslim man or woman prays in a garment on which there is some impurity (najasah), whether that garment is trousers, a shirt, a waist wrapper (izar), a sweater or anything else, and he did not remember that until after he had finished praying, then his prayer is valid according to the correct opinion. The same applies if he prays wearing an impure garment, but does not realise that until after he has finished praying. If he was unaware of that, that is also an excuse, like forgetting. So if he prays in an impure garment because he forgot or was unaware of it until he had finished his prayer, then his prayer is valid, because one day the Messenger (blessings and peace of Allah be upon him) prayed wearing shoes on which there was some dirt. Jibril informed him of that, so he took them off and he did not repeat the first part of his prayer; rather he continued his prayer. This indicates that the first part of the prayer was valid. The same applies if a person does not realise that until after he has finished his prayer; his prayer is valid because of this hadith.
The same applies to one who forgets, and the same applies to one who finishes his prayer and completes it, because he forgot or was unaware that there was some impurity on his garment or shoes. In this case is prayer is valid, but if he remembers it during his prayer, and he takes off that garment, his prayer is still valid, as the Prophet (blessings and peace of Allah be upon him) took off his shoes, then he continued his prayer. So if there is any impurity on a person’s bisht (abayah or outer garment) or on his ghutrah or on his izar, and he takes it off, but he is still wearing a garment that covers his ‘awrah, and he takes off the izar or pants on which there is some impurity straightaway, his prayer is still valid. End quote.
Shaykh ‘Abd al-‘Aziz ibn Baz (may Allah have mercy on him) – Fatawa Nur ‘ala ad-Darb (2/655).