Karkh juvenile prison in 2004. The prison currently holds 315 children, while its capacity is 250. Photograph: Marwan Naamani/AFP

Hundreds of children, some as young as nine, are being held in appalling conditions in Baghdad's prisons, sleeping in sweltering temperatures in overcrowded cells without working fans, no daily access to showers, and subject to frequent sexual abuse by guards, current and former prisoners say.

At Karkh juvenile prison, Omar Ali, a 16-year-old who has spent more than three years there, showed the multiple skin sores he and many other fellow inmates have contracted through lying on thin, sweat-soaked mattresses night after night.

"The electricity comes from a generator and it's only switched on during the two-hour weekly session when visitors come in, and for two or three hours in the evening. We are convinced the guards sell the generator fuel on the black market," he said.

Daytime temperatures in Baghdad last week averaged 44C (112F). They barely drop below 38C at night. Water supplies in Karkh are spasmodic, and Omar said he was able to shower only once every three days. Boys sleep in four dormitories, averaging 75 inmates in a cell about 5 metres by 10 metres, on double bunks or the concrete floor.

Guards often take boys to a separate room in the prison and rape them, Omar alleged. They also break prison rules by lending their mobile phones to boys to ring home, on condition that each time their families top the phone up by $10 or $20. The teaching staff resigned en masse in November because of low pay, according to an international official. As a result, the children lounge around aimlessly with no daytime activities, other than an exercise yard.

Though the boys in the prison have been convicted, international standards for fair trials are never met. "Trials last on average for 25 minutes, no witnesses are called, confessions are used as the only evidence, and court-appointed defence lawyers get the case file on the day of the trial, leaving no chance to consult the defendant in private," an international adviser in Baghdad said on condition of anonymity.

Omar Ali was 13 when interior ministry special forces raided his house in a predominantly Sunni suburb in October 2004. He and his 14-year-old brother were arrested. A week later the special forces came back and took their father. All three are still in custody.

The ministry is under Shia control and its forces have repeatedly been accused of targeting innocent Sunnis. Sahar Muhammad, the boys' mother, told the Guardian that when she was able to visit her sons they told her they were beaten repeatedly in the first days of custody and ordered to sign a blank sheet of paper on which charges would be written later.

Raad Jamal was 17 when US forces raided his home in the mixed Sunni and Shia district of Doura in June last year. His mother, Suad Ahmed Rashid, who was with him during the interview, told the Guardian: "During the US raid an American officer told my daughter: 'Tell your brother to confess he is with al-Qaida so we can send him to Camp Bucca [a US prison near Basra] or else we'll hand him to the Iraqis and they will torture him'."

Raad and a friend were taken to a US base and were transferred next morning to the seventh brigade of the Iraqi army's second regiment. Raad said he and his friend were hung from the ceiling on ropes, beaten with electric cables, and taken for interrogation one by one. "They said everyone who comes here has to confess," Raad said.

He was then sent to another Iraqi army base. "I stayed there about six months. I didn't confess anything I didn't do. They write false statements and ask you to press your thumb on it. I refused but they forced my thumb on to the paper," he said. At the juvenile court Raad encountered a sympathetic judge.

"The judge did not accept my confession. He said I was innocent but for administrative reasons I would have to go to Tobchi until I was released." He spent a few months in Tobchi and was released in March.

Last year officials from the United Nations Assistance Mission to Iraq (Unami) visited Baghdad's Tobchi prison, where children awaiting trial are held. They reported that detainees provided "particularly worrisome allegations of ill-treatment or other abuse of juvenile males, several of whom told Unami they had been beaten and sexually abused while held in the custody of the ministries of the interior and defence prior to transfer to a juvenile facility. Upon examining them Unami observed injuries consistent with beatings."

The UN found severe overcrowding at Tobchi, with around 400 inmates in a prison with an official capacity of 206. "In some cells juveniles were taking turns to sleep on the floor without mattresses," the UN reported. The ministry of labour and social affairs (Molsa), which manages the prison, said shortages of funds prevented improvements.

Kadhim Raouf Ali, deputy director general of Molsa's juvenile department, told the Guardian that inmate numbers in Tobchi had been sharply reduced this year thanks to speeded-up releases under the new amnesty law. There were only 226 inmates now. But he admitted Karkh was still overcrowded. It was holding 315 children while capacity was 250.

Child detainees in US custody in Iraq fare better than those in Iraqi hands, said Shatha Alobosi, an Iraqi woman MP. Former inmates interviewed by the Guardian confirmed that there is less overcrowding and brutality.

Now, as Iraqi pressure mounts for a return of sovereignty, the US has been moving to release all under-18s. In December last year it held 950 children. The current total is 180.

"We anticipate having less than 100 juveniles in detention by the end of Ramadan [later this month], and hopefully release all juveniles to their families before the end of this year," First Lieutenant Randi Norton, a US military spokesman, said.

The Iraqi Islamic party, the main Sunni party in parliament, takes a special interest in detainees, adult as well as juvenile, since the majority are Sunnis. It gives aid to poor families who have no breadwinner, and has urged the authorities to improve conditions and release prisoners.

"We still have a long way to go. The problem is how to make a major and drastic reform of the judicial system, and change the mentality of officers in the army and police," its leader, the Iraqi vice president Tariq al-Hashemi, told the Guardian.

· An Iraqi contributed reporting for this article. Names of inmates and family members have been changed.