"I can confirm that the two German hostages are now in safe hands," a spokeswoman said.
Thomas Nitzschke and Rene Braeunlich are now said to be with German officials in Iraq.
The pair was kidnapped on the way to work on January 24 at an oil refinery in the northern Iraqi city of Beiji.
"I am very relieved and happy that the German hostages in Iraq have been freed," Chancellor Angela Merkel said.
Foreign Minister Frank-Walter Steinmeier said in a statement he had spoken to the freed men and they were safe.
"After being held for more than three months in inhumane conditions, they are safely in German custody in Iraq, and as our plans stand now, they will return to Germany tomorrow," Steinmeier said.
A militant Iraqi group calling itself the Brigade of Supporters of the Sunna and Tawhid claimed responsibility in a video.
Videos of the pair were released a couple of times.
The first appeared several days after their capture when the militants threatened to kill them unless their demands were met.
The kidnappers had called on the German government to close its embassy in Baghdad, withdraw all German companies from Iraq and stop cooperation with the Iraqi government.
The second video of the two was released last month.
The men’s employer, Leipzig-based Cryotec Anlagenbau AG, has a commercial relationship with an Iraqi government-owned detergent company in the industrial town of Beiji, where Brazilian engineer Joao Jose Vasconcelos Jr was kidnapped on January 19, 2005.
His whereabouts remain unknown.
There had been grave fears for the two with their family and hundreds of people in their home city of Leipzig holding candlelight vigils.
Australian hurt in bombing
An Australian contractor in Iraq is recovering in a Baghdad hospital after a roadside bombing that killed his three Fijian workmates.
The bombing took place 50km south of Baghdad on Sunday, their employer, ArmorGroup said today.
The security company, based in London and McLean, Virginia in the US, said the Fijians were killed and the Australian injured when their vehicle triggered the device and overturned.
The security firm has 1,000 employees in Iraq protecting official buildings and companies.
The Australian was being treated in a Baghdad hospital, company spokesman Charlie Beese said.
The four men had been part of a convoy of at least two vehicles and passing through a village, which Beese declined to name, when the device detonated.
"A convoy was travelling through a small village when the escort vehicle triggered or was struck by the roadside bomb, killing the driver and causing it to overturn," Beese said.
"Two other men were killed and a fourth injured."
He said the dead men were Fijian nationals and former soldiers, though he said it was not known which national army the men had served in.
The employees had been protecting a convoy travelling from Baghdad to a reconstruction site south of the Iraqi capital, Beese said.
