Can you give more details about your problem? Since how long you are masturbating?. Since how long you are getting pain? Do you get the pain in the initial stages of masturbation? If you answer these questions, we can arrive at the cause of pain. Do you use lubrication? In which position you mastrurbate? Additional info may help us to answer your question .
Does this only happen when it takes a while?
I am just guessing your age to be young, and if so I would think by just this statement that you are making more *** than the current size of your urethra wants to handle.
Extended stimulation can produce more. Most men and probably you later will appreciate this.
Now if I guessed wrong, I would say that you are handling your penis long enough and rough enough that it is sore inside and responds accordingly when you ejaculate.
It would help if you stopped desensitizing yourself to stimulation, as an example, quit using porn as a stimulant. You can reach a point of ED without porn. Now while I used that as an example, the point is that if you wait for your natural sex drive to provide you stimulus you will have a better sex life.