A 21-year-old gunman opened fire at a secondary school in northern Germany on Thursday, badly injuring a female member of staff before being arrested, police said.

The incident happened at the Lloyd Gymnasium in the port city of Bremerhaven.

Investigators were still in the dark about a possible motive, but public prosecutor Oliver Constien said initial findings suggested a "specific psychological disposition may have contributed to the crime".

School shootings are relatively rare in Germany, which has some of the strictest gun laws in Europe.

But a recent spate of incidents has rattled the country.

Bremerhaven police said the suspect entered the school building shortly after 09:00 (0700 GMT) and fired at a female member of staff, who was "seriously injured".

The alarm was quickly raised and police detained the suspect at a nearby location soon after, seizing his weapon.

The injured woman is being treated in hospital, they said.

Constien said investigators were trying to clarify whether the gunman had ties to the victim.

A video circulating on social media and German news sites appeared to capture the moment the suspect was arrested.

A man dressed in black is seen lying face down on a street corner, with a weapon next to him, before being handcuffed by officers.

But there was no immediate confirmation of reports the alleged weapon was a crossbow.

According to local media, officers had raced to the scene after a female pupil rang the police from the school bathroom upon hearing gunfire, triggering a large deployment of officers and rescue workers.

School shootings are relatively rare in Germany, which has some of the strictest gun laws in Europe
School shootings are relatively rare in Germany, which has some of the strictest gun laws in Europe DPA via AFP / Sina Schuldt

Teachers and pupils followed protocol by barricading themselves in their classrooms, reports said.

By 14:00, police said special forces had completed their search of the premises and the last people had left the building.

Pupils and staff were receiving psychological counselling, they added.

Michael Frost, head of the Bremerhaven schools department, told reporters there were only around 140 people on school grounds Thursday instead of the usual 550, because of exams, excursions and external classes.

In a separate incident on Thursday, police in the eastern city of Leipzig questioned a 21-year-old still at secondary school after being tipped off by Snapchat that he had posted images of himself with a gun in a classroom and made unspecified threats.

But Leipzig police said the video turned out to be part of a school project, and the suspect only possessed an airsoft gun, a non-lethal replica firearm that shoots plastic pellets.

Last week, investigators in Essen city said they had foiled a school bomb assault after they arrested a 16-year-old who was suspected to have been planning a "Nazi terror attack".

In January, an 18-year-old student opened fire in a lecture hall at Heidelberg University in southwestern Germany, killing a young woman and injuring three others before fleeing the scene and turning the weapon on himself.

In 2009, a former pupil killed nine students, three teachers and three passers-by in a school shooting at Winnenden, in the German state of Baden-Wuerttemberg. The gunman then killed himself.

In 2002, a 19-year-old former student shot dead 16 people at a school in the central German city of Erfurt. He too then killed himself.

The Winnenden and Erfurt massacres were carried out with legal weapons and spurred Germany to tighten gun laws.

The country currently requires anyone under 25 to pass a psychiatric exam before applying for a gun licence.