Bavarian authorities said the teenager from the 13th grade was responsible for the attack just before 9 am on the Gymnasium Carolinum. Identified by the German media as Georg R., he was armed with an axe, knives and several firebombs.
The two firebombs he ignited in the attack apparently set off a fire alarm in the school, possibly heading off more carnage as students left the building and a police patrol car quickly came to the scene.
Bavarian Interior Minister Joachim Herrmann said at a press conference on Thursday afternoon that officers had to shoot the attacker upon confronting him at the school.
"With the perpetrator threatening the officers with his weapons, they were compelled to fire shots," he said. "The quick response avoided a much worse escalation."
Herrmann said a preliminary investigation showed the teen had no perceivable history of trouble, but students at the school described him as a loner.
Elke Schönwald from the police in nearby Nuremberg said he had been "seriously injured" after being shot five times and the authorities had not yet been able to interrogate him as to his possible motive.
Schönwald also said nine pupils had been hurt, including two girls who had been seriously injured. At least one is thought to have been hit directly by a firebomb.
Heavily armed police commandos searched the school before confirming the teenager had acted alone.
Authorities are on high alert in Germany after a school shooting in the town of Winnenden in March, when 17-year-old Tim Kretschmer shot dead 15 people before dying in a shoot-out with police. There have been several warnings of copycat shootings since, with armed police evacuating schools on several occasions.
In May, a 16-year-old girl was arrested after arriving at a school wearing a mask and armed with several knives, an air gun and a rucksack full of bottles of flammable liquid. She attacked another female student, nearly severing her thumb with a knife.
Story continues below…
The school in Ansbach, a picturesque medieval town, is one of the oldest in the wealthy Franconia region of northern Bavaria.
"It is definitely not a problem school," said Ludwig Unger, a spokesman for the Bavarian education ministry. "It has a very good reputation in the town."
The school was quickly evacuated by the police and its some 700 students were moved to a nearby government job centre, where parents came to pick them up.
The city of Ansbach has set up a special hotline regarding the incident: 0981/14970