Seven gunmen also died in the ensuing clashes, according to a police captain and a doctor at a hospital in the town of Hashimiyah, in Babil province. It was not immediately clear how the detainees died.
It is the second instance of a large number of detainees being killed since the start of a militant offensive on June 9 that has overrun large areas of five different provinces.
At least 44 prisoners were killed during a militant assault on a prison in the city of Baquba last week.