Welcome to puberty!
No, you haven't done anything wrong. Touching yourself and masturbating (that's what you did) is very normal and healthy.
The reason it gets hard when you kiss girls is because you are "turned on". When you are older, more mature, and ready for safe, responsible sex, the hardening penis when you kiss will make sex possible.
Unfortunately for the next few years, your penis may get hard in the most annoying places. During a math test, in gym... this is just how boys grow into men, and there isn't much you can do to control it. Every single boy on the planet goes through this!
Masturbation might actually help control those random erections during the day.