A Baghdad court has sentenced to death 24 suspected members of the Islamic State of Iraq and the Levant (ISIL), for their role in the killing of hundreds of Iraqi soldiers during their rapid advance in 2014.

The defendants, who were sentenced by hanging on Wednesday, were charged with the killings at Camp Speicher, and membership of a terror group.

All pleaded not guilty, insisting that they had not taken part in the massacre and telling the court that their confessions were coerced under torture by Iraqi officers.

"Today the Iraqi central criminal court issued a death sentence against 24 people convicted of the Speicher massacre," said Abdul-Sattar al-Birqdar, spokesman for Iraq's Supreme Judicial Council.

The case stems from the killings of 1,700 Iraqi soldiers, who were captured after ISIL overran Saddam Hussein's hometown of Tikrit in June 2014.

At the time, the soldiers were trying to flee from Camp Speicher, a nearby army base.

After Tikrit was captured, ISIL posted graphic images and videos that showed its gunmen killing scores of the soldiers, after loading the captives onto flatbed trucks and then forcing them to lay face-down in a shallow ditch.

Iraqi forces, assisted by air strikes from a US-led coalition, retook Tikrit in April, and arrested dozens of suspects said to be linked to the massacre.

Forensic teams exhumed many of the bodies from mass graves believed to contain some of the hundreds of soldiers killed by ISIL fighters.

Tears and chants

The trial in Baghdad was attended by several families of the killed soldiers, many of whom had filed claims against the defendants.

Some of the families told the judge that a day after the fall of Tikrit, they received calls from their relatives in Speicher, telling them that some Sunni tribal chiefs had arrived at the gate of the air base, and told the soldiers that they could go home safely on lorries waiting for them outside the base.

Hours later, all contact was lost with the soldiers.

At one point in the trial, while the chief judge was questioning the defendants, several relatives of the slain soldiers stormed the courtroom and started throwing shoes and water bottles at the defendants, who were inside a cage, which is customary in many Arab courtrooms.

The judge who issued the 24 death sentences also acquitted four other defendants for lack of evidence.

After the sentencing, the victims' relatives raised up pictures of their loved ones.

Some burst into tears and others chanted "Allahu Akbar," which means "God is Great" in Arabic, and "Oh, Hussein," a reference to a revered Shia imam and Prophet Muhammad's grandson.

Ali Abdul-Hamza, whose brother was among the victims, said "Justice is done" as he was leaving the courtroom.

"We are relieved to see these criminals receiving the maximum punishment," he told the AP news agency.

Ahead of the trial, Abdul-Sattar Bayrkdar, the spokesman for Iraq's Supreme Judicial Council, promised that "the trial will be transparent and fair".

He added that about 604 other fighters, believed to have taken part in the killing, were still at large.