A history teacher is being credited with preventing a former student from continuing a deadly shooting rampage at a school in eastern Germany.

A 19-year-old described as a trained marksman seeking revenge killed 16 people and wounded 10 others before killing himself Friday.

Media reports said Robert Steinhaeuser had received a gun permit just days before the attack in the town of Erfurt.

They said he was trying to avenge his expulsion from the Johann Gutenberg school for forging a doctor's note his excuse for skipping final exams.

One report said Steinhaeuser had large amounts of ammunition in his apartment and he had boasted to friends that one day his name would be famous.

Teacher Rainer Heise told Germany's ZDF television how he confronted Steinhaeuser in a hallway and dared him to "pull the trigger."

When the teen lowered his pistol, Heise said he shoved him into a classroom and locked the door.

While inside, the gunman shot himself in the head, police said. Investigators found 500 unspent rounds of ammunition near his body.

Witnesses said he was dressed in black from head to toe and wore a mask as he roamed the school.

He killed 13 teachers, one police officer and two students a 15-year-old boy and 14-year-old girl.

Police said his pistol and a pump-action shotgun were legally owned. A security official in Thuringia state said Steinhaeuser was a member of two gun clubs and had no criminal record.

Flags flew at half-mast throughout Germany Saturday. Chancellor Gerhard Schroeder visited the school, where he placed a bouquet of flowers and observed a moment of silence.