The gunman, later identified as Tim Kretschmer, killed at least 16 people.
The bloody rampage began at around 9:45 a.m. local time (08:45 GMT) when the 17-year-old gunman, dressed in black combat fatigues, entered the Albertville school in Winnenden, not far from Stuttgart, and began shooting.
The gunman, a former student at the school, managed to flee the scene, but was later surrounded by police in a supermarket some 40 kilometers (25 miles) from Winnenden. He was wounded in the leg during an exchange of fire and then shot himself in the head, according to police reports.
Earlier reports had said the gunman was killed in the shootout.
"According to our current information, the 17-year-old then shot himself," the N-24 TV channel quoted regional police chief Erwin Hetger as saying.
Police also said they had found 16 weapons that were legally held in the house where Kretschmer lived with his parents. A missing gun is believed to be the one that the teenager used in the attack.
German Chancellor Angela Merkel has described the shootings as "incomprehensible".
"This is a day of mourning for the whole of Germany," she added.