The crisis in the Bavarian city of Ingolstadt prompted Chancellor Angela Merkel to cancel an election rally she had planned there.
Officials said the 24-year-old kidnapper had previously been banned from entering city hall because he was stalking one of its female employees. The 25-year-old woman was one of the people he was holding hostage.
The kidnapper, whose identity was not released, "was shot in his shoulder and legs" and rushed to a hospital, police spokesman Guenther Beck told The Associated Press.
Two other people the captor had taken hostage, including a deputy mayor of Ingolstadt, were freed from the building earlier in the day, police said.
The kidnapper "was carrying a plastic gun which looked like a real weapon and a big knife" -- which is why police officers were forced to use their weapons, Ingolstadt police said in a statement Monday night.
Psychologists were counseling the four released hostages.
The captor was a known stalker and had been banned from entering the Ingolstadt city hall building because he had harassed the female employee there, said Bavaria's interior minister, Joachim Herrmann. The German news agency dpa said the stalking victim had been harassed for more than a year.
"The captor had an order to stay away from city hall and thought he could force his way back in to be closer to the woman he stalked," Beck said. "It was a classic case of stalking."
Police said the attacker was homeless and had been in psychiatric treatment until last month. In cases unrelated to the stalking, the man had previously been convicted of personal injury and harassment.
More than 200 police officers had surrounded city hall for nearly nine hours before the special unit raided the building. Forty firefighters and 50 paramedics also were on the scene during the standoff.
The hostage-taking happened several hours before Merkel was due in the city for a late afternoon rally that was to take place on the square in front of the city hall. Germany holds national elections on Sept. 22, and Bavaria votes in a state election a week earlier. Ingolstadt is 44 miles (70 kilometers) north of Munich and has about 125,000 inhabitants.
The old city hall where the hostages were taken is one of two city halls in Ingolstadt. It dates back to the 14th century, though the building was expanded in the 19th century. It is home to the mayor's office and the city's tourist information office, among other administrative departments.
Take ABC13 with you!
Download our free apps for iPhone, iPad, and Android devices