Crammed into a prison cell with 36 other people, the man debated his fate as he listened to the shelling.
Outside, fighters with the Islamic State in Iraq and Syria fired mortars at the anti-terrorism detention facility in Tal Afar, where fierce battles raged between the al Qaeda splinter group and Iraqi security forces.
With the facility close to being overrun by ISIS fighters, the prison guards did the unthinkable.
"At about 2 a.m., three of the guards came into our cell ... and they opened fire with a machine gun," the man said in a video obtained by CNN.
The account mirrors allegations put forward by Amnesty International, which released a report Friday saying it has evidence pointing to a pattern of "extrajudicial executions" of Sunni detainees by government forces and Shiite militias in Tal Afar, Mosul and Baquba, cities where Iraqi forces were either routed or have been locked in fierce battles with ISIS fighters and allied Sunni militants.
"The killings suggest a worrying pattern of reprisal attacks against Sunnis in retaliation for ISIS gains," said Donatella Rovera, Amnesty International's senior crisis response adviser, who is currently in northern Iraq.
The government is aware of the Amnesty International allegations but has yet to see any evidence, said Abdul-Sittar al-Birqdar, spokesman for the Supreme Judicial Council, which oversees the country's judicial system.
"At this stage, this is all just media reporting," he told CNN on Friday. "No witnesses have come forward, and no families have come forward with such claims. No one has officially asked for an investigation."
He called for the rights group to provide evidence, saying that "we are still monitoring and checking, and we will keep checking," adding that "under Iraqi law, we cannot move on it without claims and evidence."
'He was in the prison. He was killed'
Survivors and witnesses tell Amnesty International that Iraqi prison guards carried out the slaughter before Iraqi forces withdrew.
A video obtained by CNN, confirmed by Amnesty International as one of the rooms where the killings took place in Tal Afar, shows a prison cell, the floor slick with blood, where bodies of men are piled on top of one another. They appear to have fallen where they were shot.
In that same video, a woman in a black abaya with a printed head scarf stands over her brother, brushing flies from his body.
She said she found his body among others in a room in the Tal Afar detention facility.
"He was in the prison," said the woman, clearly in shock. "He was killed."
Asked how many bodies were among those with her brother, she said: "They killed a lot. I don't know."
Bodies found in Mosul
A similar scene appeared to play out at the anti-terrorism detention facility in Mosul, 50 kilometers (31 miles) from Tal Afar, Amnesty International said, where some of the dozens of bloated, twisted bodies found in a ditch were believed to have been executed by Iraqi guards before they withdrew.
A man, whom CNN is not identifying at his request, said he was being held at the same facility with about 80 others. He said he was held without charges after security forces picked him up eight months earlier.
"The guards were screaming out names. They took 15 of them and handcuffed them together," said the man, who is a university student.
Then they were taken away, he said.
Later, as ISIS fighters shelled the Mosul facility, detainees in one of the cells began chanting "Allahu akbar." That's when, the man said, one of the guards opened a cell and threw a grenade into it.
"Thank God, they were busy with the clashes, and they didn't have time to go through all the cells," he said.
The man said militants, presumably ISIS fighters, eventually released him and the other prisoners.
"They said, 'You are free. Whoever wants to go, swear allegiance to us and you can go home,' " he said.