Image copyright Facebook/Free Eddie Gallagher Image caption Eddie Gallagher is due to stand trial on 10 June

A US Navy Seal accused of stabbing a teenage Islamic State fighter to death has been unexpectedly released days ahead of his trial.

Navy Special Operations Chief Edward Gallagher, 39, is also charged with indiscriminately shooting Iraqi civilians on his eighth combat tour.

A military judge freed the combat veteran after prosecutors acknowledged tracking the defence team's emails.

US President Donald Trump is reportedly mulling a pardon for Chief Gallagher.

Image copyright Facebook/Free Eddie Gallagher Image caption Mr Gallagher and his wife, Andrea Gallagher

Thursday's surprise ruling by the judge, Capt Aaron Rugh, drew gasps in the San Diego court, according to the Associated Press news agency.

Chief Gallagher's defence team is arguing the case should be dismissed or prosecutors removed as a result of what they called illegal spying.

The highly decorated sniper and medic is scheduled to stand trial on 10 June for nearly a dozen alleged crimes.

He has pleaded not guilty to murdering an injured teenage militant in Iraq in 2017 and attempted murder for shooting civilians from a sniper's nest.

In court, lawyers for Chief Gallagher presented evidence that Navy prosecutors and their investigators had embedded a tracking code in emails sent to defence attorneys and a journalist with the Navy Times who had broken several stories on the case based on leaked documents.

Defence attorney Tim Parlatore described a "rogue, relentless and unlawful cyber-campaign" that he argued had violated attorney-client privilege and undermined Chief Gallagher's right to a fair trial, according to the Associated Press.

The lead prosecutor, Cmdr Christopher Czaplak, said the code only showed where and when recipients opened the emails.

Leaving court with her husband on Thursday, Andrea Gallagher spoke briefly with reporters saying: "We have been utterly terrorised by this entire farce of a case."

Friends in high places

By Tara McKelvey, BBC News

President Trump has taken an interest in Chief Gallagher's case, tweeting about him in a sympathetic manner and hinting that he may grant him a pardon if he is found guilty.

The president's remarks about Gallagher's case have had an impact: Mr Trump tweeted about the conditions of Chief Gallagher's confinement in March, and Chief Gallagher was transferred from a brig to a military hospital.

Being in the hospital - instead of a military jail - made it easier for him to receive visitors and speak with his lawyers.

Overall the president's interest in the case has helped to ensure that Chief Gallagher has a high profile, access to well-connected lawyers (one of them, Marc Mukasey, also works for the Trump Organization) and the tantalising possibility (at least for Chief Gallagher) that even if the jury finds him guilty, a pardon could await.

Image copyright CBS Image caption Chief Gallagher in court in January

Mrs Gallagher has called the trial "an atrocity committed against America's service members" and frequently posts updates on the Free Eddie Gallagher Facebook page.

Chief Gallagher was held in pre-trial confinement after prosecutors accused him of contacting witnesses.

His case has drawn widespread attention, including from President Trump.

At the end of March, Mr Trump personally intervened in the case, demanding that Chief Gallagher be moved from a military prison to a less-restrictive facility.

White House Officials told the New York Times that the Trump administration had made an "expedited request" last week for the paperwork needed to pardon several service members accused of war crimes, including Chief Gallagher.

Earlier in May, Mr Trump granted a full pardon to 1st Lt Michael Behenna, a former soldier who was convicted in the 2009 killing of an Iraqi man in American custody.

According to the charges, Chief Gallagher allegedly stabbed a teenage IS fighter who had been wounded in an airstrike in May 2017.

Image copyright Facebook/Eddie Gallagher Image caption Chief Gallagher's case has attracted the support of US President Donald Trump

Prosecutors say the wounded prisoner was being treated by medics from the Seal platoon that Chief Gallagher commanded when he allegedly attacked without any warning using a homemade knife.

He then had others take photos as he posed with the corpse and recited the Navy re-enlistment ceremony oath, prosecutors say.

A lawyer for Chief Gallagher said the fighter died from injuries sustained in the airstrike, and that his client is being falsely accused by Seals who wanted to get rid of their demanding platoon leader.

Cmdr Czaplak said the chief had chosen "to act like the monster the terrorists accuse us of being".