Edward Gallagher, 39, is a decorated career combat veteran

A Navy SEAL platoon leader court-martialed on war crimes charges was unexpectedly ordered released from base confinement in San Diego on Thursday, 11 days before he is due to stand trial in a case that has attracted President Donald Trump's attention.

The presiding judge, a Navy captain, freed Special Operations Chief Edward Gallagher at the end of a five-hour pre-trial hearing on defense motions stemming from his lawyers' accusations of prosecutorial misconduct.

Gallagher's team says Navy prosecutors and their investigators illegally snooped on the defense and reporters in a transgression that merits dismissal of the charges or disqualification of the lead prosecutor from the case.

The judge, Captain Aaron Rugh, adjourned the hearing for the day without ruling on those requests.

Instead, to the apparent surprise of everyone present, the judge declared, "As an additional remedy to the interference from the prosecution, I order the release of Chief Gallagher."

Many in the courtroom gasped, including the defendant's wife, Andrea Gallagher, who burst into tears.

Her husband, dressed in his Navy summer whites uniform, smiled broadly before leaning over to hug and kiss his wife.

The couple left the Navy courthouse holding hands, surrounded by members of the defense team.

While Gallagher declined comment on the advice of attorneys, his wife spoke briefly with reporters, saying, "We have been utterly terrorized by this entire farce of a case."

The proceedings at Naval Base San Diego were set to resume on Friday.

Gallagher, 39, a decorated career combat veteran who has pleaded not guilty to all charges, had been restricted to base at the nearby Naval Medical Center San Diego since late March.

He was transferred there from a military brig at the Marine Corps Air Station Miramar in California at the direction of Trump, who ordered less-restrictive confinement while Gallagher awaited trial "in honor of his past service to our country."

Possible pardon?

Trump has said he is considering pardons for a number of service members accused of war crimes, and media reports have cited U.S. officials as saying Gallagher's is one of the cases under review.

One of Gallagher's civilian defense attorneys, Marc Mukasey, also serves as a personal lawyer for Trump.

Jury selection is set to begin June 10 in the court-martial accusing Gallagher of fatally stabbing a helpless, wounded Islamic State fighter in his custody and of shooting two unarmed civilians, a schoolgirl and an elderly man in Iraq in 2017.

If convicted of all charges, including murder, attempted murder and obstructing justice, he could face life in prison.

Gallagher says he was wrongly accused and that fellow SEAL team members testifying against him, several under grants of immunity, are disgruntled subordinates who fabricated allegations to force him from command.

His defense team has argued the case should be dismissed altogether on grounds of alleged misconduct by the Navy prosecutor and agents of the Naval Criminal Investigative Service (NCIS).

The defense specifically has accused Navy lawyers of conducting illegal surveillance of defense attorneys and news media using electronic tracking software secretly embedded in emails sent to the defense.

In court, prosecutors have said the email "auditing tools" they used were designed merely to detect the flow of emails without revealing their content, and were aimed at pinpointing the source of leaks from case files sealed by the judge.

According to testimony on Thursday, prosecutors also assembled dossiers on four defense lawyers and a Navy Times reporter, assigning a counter-intelligence clerk to obtain personal information on the five from at least one military database.

The judge has said he did not ask for or order any investigation of leaks. On Thursday, the defense withdrew its request for the judge's recusal.