Militants attacked two Iraqi prisons including the notorious Abu Ghraib in a bid to free inmates, sparking all-night clashes in which at least 41 people died, officials said on Monday.
The coordinated attacks on Taji prison, north of Baghdad, and the Abu Ghraib facility, west of the Iraqi capital, were launched on Sunday night and raged for around 10 hours, officials said.
A police colonel said seven inmates escaped from Abu Ghraib during the clashes but were later arrested, while jihadists claimed on the internet that thousands of prisoners were freed.
Officials said at least 20 members of the security forces were killed and 40 wounded in the attacks.
Justice ministry spokesman said that 21 inmates were killed and 25 wounded during rioting at the prisons, without specifying whether they were bystanders or taking part in the fighting.
It was not immediately clear how many of the militants who attacked the prison were killed, wounded or captured.
The attacks were launched at around 9:30pm (6:30pm GMT) on Sunday when the gunmen fired mortar rounds at the prisons.
Four car bombs were detonated near the entrances to the jails, while three suicide bombers attacked Taji prison, said the police colonel.
Five roadside bombs also exploded near the prison in Taji.
Fighting continued throughout the night as the military deployed aircraft and sent in reinforcements around the two facilities.
The situation was eventually brought under control on Monday morning, according to the colonel.
"The security forces in the Baghdad Operations Command, with the assistance of military aircraft, managed to foil an armed attack launched by unknown gunmen against the... two prisons of Taji and Abu Ghraib," the interior ministry said in a statement late on Sunday night.
"The security forces forced the attackers to flee, and these forces are still pursuing the terrorist forces and exerting full control over the two regions," it said.
But comments posted on social networking websites, including some Twitter accounts apparently operated by jihadists, said thousands of prisoners had escaped.
The attacks on the prisons came a year after al Qaeda's Iraqi front group announced it would target the Iraqi justice system.
"The first priority in this is releasing Muslim prisoners everywhere, and chasing and eliminating judges and investigators and their guards," said an audio message attributed to the group's leader, Abu Bakr al-Baghdadi, in July last year.
Prisons in Iraq are periodically hit by escape attempts, uprisings and other unrest.
Abu Ghraib became notorious after photographs showing Iraqi detainees being humiliated and abused by their US guards were published in 2004.
It also served as a torture centre under executed dictator Saddam Hussein's ousted regime.
Deadly violence also hit security forces in northern Iraq on Monday.
A suicide car bomber attacked an army patrol in the city of Mosul, killing 12 people and wounding 16, while a roadside bomb wounded a soldier and a civilian near the city.
Iraq has faced years of attacks by militants, but analysts say widespread discontent among members of its Sunni minority, which the government has failed to address, has fuelled the surge in unrest.
With the latest attacks, more than 590 people have been killed in violence so far this month and over 2,800 since the beginning of the year, according to AFP figures based on security and medical sources.