German chancellor Angela Merkel was forced to scrap an election rally in the Bavarian town of Ingolstadt when a gunman took a deputy mayor and two of his staff hostage for more than nine hours.

The assailant, described by police as a stalker who suffered from psychological disorders, was arrested at the end of a tense standoff at the city hall. He had been injured.

Armed with a handgun, the man entered the 14th-century building and took three people hostage. Police cordoned off the building and negotiated with the hostage-taker via phone.

Deputy mayor Sepp Misslbeck was released after five hours but his two assistants - a man and a woman - were held at gunpoint inside his first-floor office until police smashed their way in to release them.

The gunman was described as a 24-year-old who was issued with a restraining order for stalking a 25-year-old-woman who worked in the building, Bavaria's state interior minister, Joachim Herrmann, said.

German tabloid Bild suggested that the woman, a secretary to Misslbeck, was the female hostage.

During the standoff mayor Alfred Lehmann said the hostage-taker wanted them to "reverse a decision" although it not clear what decision he meant.

Merkel was due to hold an election rally outside the Ingolstadt city hall but canclled. There was no indication of a connection between her visit and the hostage-taking.

Germany is to hold national elections in September.

Bavaria gunman hostage city hall
Police armoured vehicles stand in a side street during a hostage situation, in Ingolstadt, Bavaria.
hostage germany
Emergency vehicles stand in a cordoned off street in front of Ingolstadt city hall (Reuters)