Yes, what you did was correct and in accordance with sharee’ah. Purity is a condition of prayer being valid, and janaabah necessitates ghusl. The prayers that you offered whilst junub are excused because you did not do that deliberately, but you did not discharge your duty after you realized what your situation was until you did ghusl and repeated the prayers. This is what you did and this is correct.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) was asked about someone who noticed semen on his clothes after he had prayed Fajr, and he had not noticed it before. What is the ruing on that?
If he did not go back to sleep after praying Fajr then his Fajr prayer is not valid because he did it when he was junub, as he can be certain that it happened before he prayed.
But if he went back to sleep after praying Fajr, then he does not know whether this happened during his sleep after the prayer or during his sleep before the prayer, so the basic principle is to assume that it happened after the prayer and that the prayer is valid. The same ruling applies if a person notices traces of semen and is not sure whether that is from the previous night or the night before that, so he should assume that it is from the previous night and that it happened in the last period of sleep, because that is more certain, whereas there is some doubt concerning what came before that, and if there is doubt concerning something that invalidates one’s state of purity there is no obligation to purify oneself from it. The Prophet (S) said: “If one of you feels something in his stomach and is not sure whether something (i.e., wind) came out of him or not, then he should not leave the mosque.” Narrated by Muslim from the hadeeth of Abu Hurayrah (may Allaah be pleased with him). And Allaah is the Source of strength.
Majmoo’ Fataawa al-Shaykh Ibn ‘Uthaymeen, 11/question no. 165.
The Shaykh was also asked:
A person prayed Maghrib and ‘Isha’, then he went home. When he took off his clothes he noticed traces of semen on his underwear. What should he do?
If this man who found semen on his clothes did not do ghusl, then he has to do ghusl and repeat the prayers that he offered when he was junub. But sometimes a man may see traces of janaabah on his clothes and not know whether that happened the previous night – should he regard that as having happened the night before or on a previous night?
The answer is that he should regard it as having happened on the night before, because there is some doubt concerning the previous night, so the basic principle is that he was in a state of purity (on the previous night). Similarly if he went to sleep after praying Fajr then woke up and noticed traces of janaabah on his clothes, and he does not know whether that was from his sleep after Fajr or his sleep during the night, should he repeat Fajr prayer?
The answer is that he does not have to repeat Fajr prayer, because there is no certainty that he had a wet dream whilst he slept during the night. So you should make this the rule you follow: every time you are uncertain as to whether this janaabah happened during a previous sleep or a later sleep, assume that it was during the later sleep.
Majmoo’ Fataawa al-Shaykh Ibn ‘Uthaymeen (11 Shawwaal, no. 166).
And Allaah knows best.