Masturbation involves lust, no denying that. Also, it is a bout selfishness. Masturbation is all about fulfilling your own selfish desires to please yourself and putting your personal lusts over guarding your heart against lust. Sex is designed to be between two people. Man and woman. Married. Replicating sex without a woman or marriage really sounds acceptable?
Also, in regards to Onan, if you read that in context, those commands are only for him in that particular situation and are not meant for all people.