One policeman and eight gunmen were also killed in clashes that erupted during the attack in the Hashimiyah area of Babil province, according to a police captain and a doctor. 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 major 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.
Accounts differed as to who was responsible for the Baquba killings, with Prime Minister Nuri al-Maliki's security spokesman saying the prisoners were killed by insurgents carrying out the attack, and other officials saying they were killed by security forces as they tried to escape.