BAGHDAD -- The man accused of masterminding an attack on a Baghdad church last year wrestled a gun from a guard at a detention facility, freed his comrades and launched an hours-long assault that ended with 17 people dead, including a top counterterrorism officer, officials and witnesses said.

Abu Huthaifa al-Battawi, the man accused by authorities of plotting the October attack on an Iraqi church killing 68 people, nearly drove out of Baghdad's Ministry of Interior with fellow inmates before being gunned down by guards.

The melee at the sprawling compound raises questions about how a group of prisoners at what is supposed to be one of the most secure facilities in the country managed to launch such a fierce attack.

The detainees, all accused of belonging to Al Qaeda in Iraq, were being moved from a detention room to an interrogation room at the ministry grounds in eastern Baghdad when one of the detainees attacked a guard and wrestled away his weapon, said Maj. Gen. Qassim al-Moussawi, the top military spokesman in Baghdad.

Two interior ministry officials said it was al-Battawi who launched the attack. One official said al-Battawi's hands had just been untied for his interrogation when he grabbed the weapon.

The detainee killed the guard, moved into one of the rooms and killed another guard and took his weapon, al-Moussawi said. In all, the detainees managed to seize four weapons including an assault rifle.

An Iraqi lawmaker on the security and defense committee, Hakim al-Zamili, said they also managed to get their hands on grenades.

The prisoners then entered the office of Brigadier Muaeid Mohammed Saleh, the head of a department responsible for combating terrorism and organized crime in eastern Baghdad, and shot him along with another officer who was in the room.

"I was in the next room close to Brigadier Muaeid's room and I heard shots fired and screams in the corridor. I opened the door and saw about four Al Qaeda detainees moving around and I closed the door back immediately," said Saleh's bodyguard, Jawad Kadhum.

"Then I heard one of them saying 'This is the director's room,' and I heard a flurry of gunshots," he said.

Al-Moussawi said the assault by the prisoners was not spontaneous but appeared to have been plotted ahead of time. He said six police and 11 detainees were killed in the ensuing melee which lasted for nearly three hours before Iraqi security forces brought the situation under control.

A group of the detainees, including al-Battawi, managed to seize a car and were driving toward the gate of the compound when a guard opened killed them with a machine gun, al-Zamili said.

Al-Moussawi said the detainees were not shackled at the time, which is normal procedure except that since they were accused of being involved in Al Qaeda, they should have been restrained.

"I blame the security measures in this case because they were senior terrorists," he said. "Tight security measures should have been taken."

An Interior Ministry official on the scene said the guards violated procedure by keeping their weapons with them when moving the prisoners. Usually when prisoners are taken into the investigation room their restraints are removed but guards are not supposed to have their weapons on them at the time.

The official said about 20 to 25 prisoners were involved in the melee.

An additional eight police officers and six detainees were wounded, security and hospital officials said.

The injured detainees were brought to Baghdad's al-Kindi hospital under tight security, treated and then taken away again by security officials to an unidentified location, officials said.

All the officials spoke on condition of anonymity because they were not authorized to speak to the media.

It is the latest embarrassing incident for Iraq related to its detention facilities.

In January, 12 inmates, many believed to have links to Al Qaeda in Iraq, were awaiting trial in a temporary detention center in the southern city of Basra when they obtained uniforms and walked out in disguise. They scattered after that to avoid the massive manhunt. At least two were later picked up by security officials in northern Iraq.

Sunday's prison attack immediately led to cries of outrage over how such an incident could have happened.

"The Interior Ministry lets large number of dangerous terrorist leaders gather in one cell without any means of surveillance such as cameras and this makes them free to plot and even give orders to people outside to carry out attacks through mobiles smuggled to them in the prison," al-Zamili said.