The remaining two hostages were freed in the town hall of the southern German city of Ingolstadt as police raided the building after hours of standoff on Monday.
The gunman, who had taken three hostage, released Deputy Mayor Sepp Misslbeck earlier after negotiations.
The man entered the building shortly before 9:00 a.m. (0700 GMT). Police cordoned off the building and contacted directly with the hostage taker.
Shots were heard when police raided the building. The remaining two hostages, including the deputy mayor's secretary, were freed unhurt shortly before 6:00 p.m. (1600 GMT).
The offender was shot in the shoulder and leg, the German media reports said.
Bavarian Interior Minister Joachim Herrmann told reporters that the 24-year-old hostage-taker was stalking the deputy mayor's secretary who had finished a relationship with him, which led to his sentence of one year and eight months of probation.
According to the local newspaper Donaukurier, the offender was undergoing psychiatric treatment.
Police said the motivation of the hostage-taker remained unclear and they believe that the incident has nothing to do with Merkel's visit.
German Chancellor Angela Merkel was scheduled to visit the Bavarian city for an election rally at 17:00 (1500 GMT). The plan was canceled due to the incident, announced her Christian Democratic Union (CDU) and its sister party Christian Social Union (CSU) in the southern German state.
Ingolstadt, known as the headquarters of carmaker Audi, is a city of 125,000 people near Munich.