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, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
The majority of scholars hold the view that touching one's penis without a separating medium invalidates the Wudhoo’. Apart from Abu Haneefah, who adopted a different view; this is also the opinion of Maalik, Ash-Shaafi‘i and Ahmad, may Allaah have mercy upon them. The majority quoted the Hadeeth narrated on the authority of Busrah bint Safwan, may Allaah be pleased with her, which stated that the Prophet, sallallaahu ‘alayhi wa sallam, said: "Whoever touches his penis should redo Wudhoo’." [Abu Daawood and others]
There is another Hadeeth in which the Prophet, sallallaahu ‘alayhi wa sallam, said: "If one of you touches his genitals without a covering or a veil, he must perform Wudhoo’." [Ibn Hibban, Ad-Daraqutni, Al-Bayhaqi and Al-Hakim; Al-Albani: authentic]
According to Ahmad ibn Hanbal and Al-’Awza‘i, may Allaah have mercy upon them, there is no difference between touching the penis with the palm of the hand or its back. Ibn Qudaamah Al-Maqdisi, may Allaah have mercy upon him, said:
"There is no difference (regarding the ruling of touching the penis) between the palm of the hand or its back. This is the opinion of ‘Ata’ and Al-’Awza‘i. Maalik, Ash-Shaafi‘i and Is-haq said, "It (ablution) is not invalidated unless the penis is touched by the palm of the hand."
Given the opinion of the majority, you should beware of doing this in the future because every prayer that you performed with the Wudhoo’ which was followed by touching your penis without a separating medium is invalid. So making up for the past prayers is due. If you know the number of the prayers you have performed in such a state, you have to make up for them. If you do not know, you have to pray until you believe that you are no longer liable. Nevertheless, you are blameless for that since you were ignorant unless you were able to learn and neglected doing so. In such a case, you are guilty and you are required to repent. That is because a Muslim has to learn what is necessary for the validity of his worship and this is an individual obligation.
Given the opinion of Abu Haneefah, may Allaah have mercy upon him, touching the penis does not invalidate the Wudhoo’ and thus your prayer is valid. Talking about the invalidators of the Wudhoo’, he mentioned in Maraqi Al-Falah Fi Sharh Noor Al-Eedhah, a book in the Hanafi Fiqh: "Touching the penis, the anus and the genitals in general." However, following the opinion of the majority is more cautious.
Allaah Knows best.