A man armed with a large knife has killed three people and injured several others in the city of Wurzburg before being shot by police and arrested, German authorities said.

Police identified the suspect as a 24-year-old Somali man living in Wurzburg. His life was not in danger from his gunshot wound, a spokeswoman said.

Bavaria’s top security official Joachim Herrmann said the injured include a young boy, whose father was probably among the dead.

The suspect was in psychiatric treatment before the attack and had been known to police, Mr Herrmann said.

Videos posted on social media showed pedestrians surrounding the attacker and holding him at bay with chairs and sticks in the centre of the Bavarian city.

Germany Attack
Police were alerted to the knife attack at 5pm on Friday (Karl-Josef Hildenbrand/dpa via AP)

A woman who said she had witnessed the incident told German RTL television that the police then stepped in.

“He had a really big knife with him and was attacking people,” Julia Runze said. “And then many people tried to throw chairs or umbrellas or cellphones at him and stop him.”

“The police then approached him and I think a shot was fired, you could hear that clearly.”

Police spokeswoman Kerstin Kunick said officers were alerted at around 5pm to a knife attack in Barbarossa Square in the centre of the city.

Germany Attack
Armed police have been on patrol (Karl-Josef Hildenbrand/AP)

Bavaria’s governor Markus Soeder expressed shock at the news of the attack. “We grieve with the victims and their families,” he wrote on Twitter.

Police said on Twitter that there was no danger to the population.

Wurzburg is a city of about 130,000 people located between Munich and Frankfurt.