Navy Secretary Richard Spencer was unexpectedly fired Sunday night over actions related to Navy SEAL Edward Gallagher, who President Donald Trump cleared of a conviction in a high-profile war crimes trial but has remained in the Navy's crosshairs.

Defense Secretary Mark Esper asked for Spencer's resignation "after losing trust and confidence in him" after secret negotiations with the White House were uncovered, the Defense Department said in an emailed statement.

While the Navy secretary publicly supported SEAL leadership's plan to have Gallagher stand before a Trident Review Board, Spencer privately assured the White House that the Navy SEAL would retire with both his rank and Trident pin.

The review board is held by fellow SEALs and their recommendation then passes to Naval Special Warfare Command and Navy Personnel Command before its approval by top service leaders.

"I am deeply troubled by this conduct shown by a senior DoD official," Esper said in a statement. "Unfortunately, as a result I have determined that Secretary Spencer no longer has my confidence to continue in his position."

On Monday, the Pentagon said Esper had barred SEAL leaders from launching a Trident Review Board for Gallagher.

"Secretary Esper's position with regard to [Uniform Code of Military Justice], disciplinary, and fitness for duty actions has always been that the process should be allowed to play itself out objectively and deliberately, in fairness to all parties," the DoD statement said. "However, at this point, given the events of the last few days, Secretary Esper has directed that Gallagher retain his Trident pin."

In his resignation letter, though, Spencer made it sound as though he was resigning over a disagreement with the president, thus contradicting the Pentagon's narrative. Spencer is a former U.S. Marine who worked on Wall Street after his service.

While Gallagher was previously cleared of the most serious charges of murder and attempted murder, he was found guilty of taking photos with the body of a deceased teenage militant. For that he was demoted. Members of Gallagher's platoon also posed with the corpse, raising concerns about good order and discipline under Gallagher's authority.

President Trump recently intervened, pardoning Gallagher and restoring his rank.

This past week, news reports revealed that the Navy intended to kick Gallagher out of the SEALs. These reports prompted the president to tweet that the Navy would "NOT be taking away Warfighter and Navy SEAL Eddie Gallagher's Trident Pin."

"As Secretary of the Navy, one the most important responsibilities I have to our people is to maintain good order and discipline throughout the ranks," Spencer wrote in his letter. "Unfortunately, it has become apparent that in this respect, I no longer share the same understanding with the Commander in Chief who appointed me."

"I cannot," he said, "in good conscience obey an order that I believe violates the sacred oath I took in the presence of my family, my flag and my faith to support and defend the Constitution of the United States."

Trump also characterized the Navy secretary's ouster as a disagreement.

"I was not pleased with the way that Navy Seal Eddie Gallagher's trial was handled by the Navy. He was treated very badly," Trump wrote on Twitter, adding that "large cost overruns from past administration's contracting procedures were not addressed to my satisfaction."

It is unclear to what the president is referring to when he talks of "cost overruns," but Trump has repeatedly expressed concerns about the aircraft carrier USS Gerald R. Ford, which is way behind schedule and over budget at $13 billion.

The Navy secretary had previously bet his job on having the Advanced Weapons Elevators on the Ford working by the end of October, telling the president that if he failed, Trump could fire him. When the Navy failed to complete the work on time, he was not fired.

Spencer told reporters that the president understood that it was a complex system.