My view of this is quite clear: you’re doing nothing wrong. Sexual thoughts aren’t “bad”, provided you don’t follow them obsessively; neither is masturbation “bad”.
Still, I don’t want to encourage you to do something you’re uncomfortable with so you may want to spend some time asking yourself why you think this is a bad thing.
Do you have someone you can trust (and I mean really trust) who you can talk about this with?