Edward Gallagher has been charged with killing a captive teenage terrorist while deployed to Iraq.

A witness in the court-martial trial of a decorated US Navy SEAL charged with killing a captive teenage terrorist while deployed to Iraq confessed on Thursday that it was he -- not the defendant -- who put an end to the boy's life.

Corey Scott, a first class petty officer, told the military court in San Diego that while he had seen Special Operations Chief Edward Gallagher stab the wounded ISIS group fighter in the neck in May 2017, he had killed the boy afterwards.

He testified that he covered the victim's breathing tube with his thumb and then watched him die.

Scott said he did so to spare the boy -- who prosecutors say was about 15 years old -- from suffering or being tortured by Iraqi forces.

His bombshell testimony threw a wrench in the prosecution's murder and attempted murder case against Gallagher, who was supposed to be providing medical treatment for the insurgent when he allegedly stabbed him.

Gallagher, 40, has denied the charges against him, claiming they were made up by disgruntled subordinates under his command.

Prosecutors on Thursday argued that Scott's version of events was a fabrication and that he was lying to protect Gallagher.

Scott, who has been given immunity from prosecution, acknowledged during questioning that he made the revelation to spare Gallagher, who is married and has children, from going to prison.

Local news reports said Gallagher appeared jubilant following Scott's testimony, celebrating with his family outside the courtroom.

The charges against Gallagher, who is also accused of shooting two civilians and firing indiscriminately at other non-combatants while in Iraq, stem from testimony by men who were under his command in the special operations branch of the US Navy.

"Boasted about killing"

According to court documents, some members of the "Alpha" platoon were so horrified by Gallagher's behavior that they tampered with his sniper rifle and fired warning shots to scare civilians away before he had time to open fire on them.

They told investigators that Gallagher, who began his career as a medic, would brag about the number of people he had killed.

One platoon mate who testified on Wednesday said he saw Gallagher fatally stab the teenage ISIS terrorist. Gallagher allegedly then posed with the boy's body for photographs and texted the pictures to fellow SEALs.

"Good story behind this one," one text message read, according to prosecutors. "Got him with my hunting knife."

When some of the other SEALs expressed reservations over the killing, prosecutors say Gallagher's response was: "I thought everyone would be cool with it. Next time it happens, I'll do it somewhere where you can't see."

Gallagher also stands accused of attempted murder for the wounding of two civilians, a schoolgirl and an elderly man, who were shot while walking along the Tigris River.

The case has proven divisive in the US, where he remains a war hero to some.

His cause has been championed by around 40 Republican members of Congress, as well as the Fox News channel, which is popular among conservatives.

US President Donald Trump last month also expressed support for Gallagher and hinted that he may be pardoned along with other military service members accused of war crimes.

The trial, which began on Monday, is expected to last up to three weeks.

If convicted, Gallagher faces up to life in prison.