They happen because of bullying, mental instability, and not a clear understanding by the people who run the school (principals, society, etc.)
I was extensively bullied in high school. I was considered by many to be a freak, and some people were scared of me. I gradually became popular in my last two years of high school, but still was considered "weird."
If I didn't have music to fall back on (the reason I became cool was because of my music; I even got a song I wrote played over the broadcast system during morning announcements, I was advertising t shirts), I probably would have still been an outcast. I know how to shoot a gun, learned it in boy scouts, but would never kill anyone. And I hate guns, anyway. However, I am not everyone, and some people just have that line blurred by video games or what have you, and end up shooting people.
Sociopaths can be teenagers, too. And for every school shooting, there is an isolated incident like that gym shooter 3 years ago. George Sodini. Some people are sick, twisted, and demented.