All perfect praise be to Allah, The Lord of The Worlds, and may His peace and blessings be upon our Prophet Muhammad and upon all of his family and companions.
In general, masturbating is forbidden, so you (Questioner) should make repentance and abstain from it to preserve your religion and health.
This bad habit will do substantial damage to your sexual life in marriage.
In conclusion, whosoever discharged sperm should perform Ghusul (Ritual bath) as ablution alone won't suffice, because a major impurity can't be removed save with Ghusul.
Therefore, you (Questioner) should perform Ghusul and redo the prayers you have performed while in state of impurity. And Allah Knows Best.