A 23-year-old man admitted Thursday to stabbing to death one of the teachers at his former school in an attack that sparked a major police operation, authorities said.

The man, armed with a pistol and a hunting knife, said he had killed the teacher at a training college in Ludwigshafen in southwestern Germany because he was angry about being given "bad marks", a prosecutor said.

The attack saw armed police commandos rush to the college as its 3,200 students were evacuated in the latest in a string of similar incidents in Germany.

"The accused has admitted attacking the 58-year-old teacher," state prosecutor Lothar Liebig said after the man was arrested.

"He said his motive was because of the great anger he felt towards this teacher for giving him very bad marks."

Police arrived at the training college after a fire alarm was set off and found all the students assembled in the playground. They were told that an armed man was on the loose.

"They went up to the third floor and saw a man fleeing, shooting his handgun. The four police, guns drawn, told him to drop his weapon ... which he then did," Franz Leidecker from Rhineland-Palatinate state police said.

Police later found the teacher lying seriously injured on the cellar steps of an adjacent building.

"He was still alive, but obviously badly hurt. He died shortly afterwards," Leidecker said.

Prosecutors said the weapon was a blank-firing pistol. The 23-year-old, who has not been identified, was due to appear before an investigating judge on Friday on suspicion of murder, Liebig said.

Dramatic television pictures showed dozens of green-uniformed police at the scene carrying machine guns. Police helicopters, ambulances and firemen were also deployed.

The college cancelled classes on Friday and Saturday, and said pupils would be offered counselling.

Security has been tightened at schools across Germany after 17-year-old Tim Kretschmer went on a rampage at his former school last March, killing 15 people before turning his gun on himself in a shoot-out with police.

He killed nine pupils and three teachers, and then hijacked a van and forced the driver to travel 40 kilometres (25 miles) with a gun in his back, telling him what he had done was "fun".

Kretschmer shot dead three more passers-by in another town before being cornered by police, when he turned the gun on himself.

Chancellor Angela Merkel and President Horst Koehler attended a memorial service in the town, beamed out on some 20 giant screens to the 7,500 people outside, as flags flew half-mast around the country.

There have been similar incidents since then, including in May when a 16-year-old girl tried to set her school on fire with Molotov cocktails and attacked a fellow pupil with a knife.

In September an 18-year-old went on the rampage at his school, lobbing Molotov cocktails and injuring 10 people, before being shot and arrested.

The country's worst school massacre was in the eastern city of Erfurt in 2002 when 19-year-old Robert Steinhaeuser killed 16 people and then himself.

- AFP