A few things happen that makes masturbating feel good.
First, the penis has a very large number of nerve endings, which means that it's very sensitive to touch. Us Humans are social creatures, so touch generally feels good, and so this feeling is amplified by the large number of nerves in the penis.
Another thing in order to promote procreation, our brains release chemicals that include endorphins and oxytocin during and after sex, which stimulate the brain in a positive manner.
Our body can't really distinguish what is actually stimulation your penis (ie hand, mouth, or vagina), so the chemicals get released whenever our penis is stimulated to the point of orgasm.