The police said the woman responsible for the massacre had also been killed.
"The situation is now under control," said a police spokesman.
The shooting started at 6:00 pm (1600 GMT) at Saint Elizabeth hospital in the centre of Lörrach, which lies in Baden-Württemberg near Germany's borders with France and Switzerland.
One police officer, who happened to be in the hospital as the woman began shooting, suffered life-threatening injuries. She also attacked the officers arriving on the scene before she was shot and killed.
The authorities believe the shooter left a nearby apartment house where there was an explosion shortly before the massacre. A dead adult and child were later found in the building.
"At around 1600 GMT, firefighters were called out to an explosion at an apartment in a street near the hospital," the spokesman told news agency AFP.
"Soon afterwards, firefighters found two bodies in the apartment."
The incident comes only days after the manslaughter trial opened in Stuttgart Thursday of the father of Tim Kretschmer, the teenager who shot dead 15 people before killing himself at his old school in Baden-Württemberg in March 2009.
Jörg Kretschmer, a 51-year-old businessman, is accused of having violated gun laws because his 17-year-old son was able to take his 9mm Beretta pistol from his bedside to use in the shootings in the town of Winnenden.