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 is His slave and Messenger.
The prayer and ablution do not become void just because of having an erection but they become void if there is a discharge of Al-Mathy (a white, thin and sticky fluid which is discharged due to sexual stimulation).
So, if the praying person is certain that he discharged Al-Mathy during the prayer, then his ablution and prayer become void but if he is having just doubts and is not sure, then his ablution and prayer are not void because in principle he is pure [in a state of ablution] as certainty cannot be removed by doubt.
If erection takes place during the prayer but the discharge of Al-Mathy happened after the prayer, then the prayer is valid. Also, if he discovers that after the prayer and he does not know whether the discharge happened during the prayer or after it, and it is possible that this could have happened after the prayer, then the prayer is valid because the general rule is attributing an incident to the most recent time. One of the Shaafi’i books (Nihaayat Al-Muhtaaj) reads: “If it is probable that it (the discharge, impurity) happened after the prayer, then it does not make the prayer void as the incident should be attributed to the closest time to the present time (i.e. to the time of the prayer).”
The same thing is mentioned by Al-Bujairimi in his comment on Al-Khateeb as he said: “His saying (If it is probable that it happened) means either it is preponderant (to happen) or not preponderant, or that both possibilities are equal ….then it is not an obligation to make up the prayer but it is a Sunnah (i.e. recommended) to do so as stated by An-Nawawi in his book Al-Majmoo’.” For more benefit, please refer to Fataawa 90814 and 121702.
Allaah Knows best.