A police operation ended the confrontation leaving the 24-year-old hostage-taker slightly injured and his two remaining captives – including a woman he had been stalking – unharmed.
The man, a psychiatric patient who had carried either a real or imitation handgun, had entered the town hall of Ingolstadt north of Munich at around 9 am, initially taking four hostages.
Police and a psychiatrist kept negotiating with the man, whose only demand was a doner kebab, throughout the afternoon.
The man freed one female captive almost immediately, and a male hostage five hours later when he released the town's deputy mayor, Sepp Misslbeck.
More than 200 police had cordoned off the historic town centre of Ingolstadt, where campaign posters still advertised Merkel's cancelled campaign event ahead of September 22 national elections.
News came shortly before 6pm, after shots had been heard, that the drama had ended with the two remaining captives freed.
The hostage-taker, who police said was slightly wounded, was taken away in an ambulance, said the regional Donau Kurier daily in an online report.
"It's a horrible situation," town Mayor Alfred Lehrmann had said earlier. "The staff were deeply dismayed and didn't know how to react... It's a very bad day for Ingolstadt."
Lehmann said the man had a history of violent assault and intimidation, adding that the term "stalker" seemed too harmless. The man was a former friend of the deputy mayor's receptionist who had been barred from contacting her by a court order but approached her anyway, local media reported.
Police arrested him in mid-2012 and he was placed in psychiatric care. A trial that ended last month, with other unspecified charges, led to a suspended sentence of one year and eight months.
The man had already entered the town hall building earlier this month, which led to him being barred from the building, said the Bavarian interior minister, Joachim Herrmann.
Merkel had been due to speak at an afternoon rally of her conservative Christian Democratic Union outside the town hall, along with her political ally, Bavarian state premier Horst Seehofer, who is also running for re-election next month.
The hostage drama was not believed to be related to the political event, said a police spokesman.
Merkel's conservative party and its Bavarian sister party cancelled Monday's appearance in Ingolstadt as well as a separate event in the city of Regensburg.