(CNN) In an extraordinary move, the Pentagon chief "fired" the Navy secretary Sunday for going outside his chain of command by proposing a "secret agreement with the White House," according to a senior defense official.

The agreement that led to Navy Secretary Richard Spencer's forced resignation involved the case of Navy SEAL Eddie Gallagher , one of three service members facing war crimes allegations whose cases have caused unprecedented tension between the Pentagon and President Donald Trump.

Spencer had appeared to be seeking a way to resolve a standoff between the Pentagon and White House over Gallagher's case , but competing narratives that emerged in the chaotic hours after Spencer's dismissal suggest the depth of the upheaval, disconnection and discord that remains.

Trump suggested on Twitter that Spencer's dismissal had to do with cost overruns and the way Gallagher had been treated by the Navy. Defense Secretary Mark Esper said he asked Spencer to resign because he had lost "trust and confidence in him regarding his lack of candor," according to a Pentagon spokesman.

And Spencer pointed squarely to the President, suggesting that Trump was undermining the "key principle of good order and discipline" of the US military by intervening in Gallagher's case.

"I cannot 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," Spencer wrote in his letter to the President acknowledging his termination

Gallagher had been convicted of bringing discredit to the armed services after posing next to a dead ISIS fighter's body, which is against regulations. He was demoted for that offense and acquitted of a separate murder charge. After Trump reversed Gallagher's demotion a week ago, military officials launched a formal review to determine if Gallagher was fit to serve, which is protocol after a conviction. That review was expected to lead to his expulsion.

Gallagher issued a statement on Sunday evening thanking his family, legal team and supporters that ended by heaping praise on Trump.

"President Donald Trump you have my deepest gratitude and thanks. You stepped in numerous times and showed true moral fiber by correcting all the wrongs that were being done to me. You are a true leader and exactly what the military and this nation needs," Gallagher said.

A shocking twist

Spencer's departure is the latest shocking twist in an ongoing standoff between the Pentagon and Trump over Gallagher and the two other service members -- a standoff that looks set to aggravate tensions between the President and Democrats in Congress.

Senate Minority Leader Chuck Schumer released a statement Sunday night saying he had spoken with Spencer and offered support.

"Secretary Spencer did the right thing and he should be proud of standing up to President Trump when he was wrong, something too many in this administration and the Republican Party are scared to do," Schumer said. "Good order, discipline, and morale among the Armed Services must transcend politics, and Secretary Spencer's commitment to these principles will not be forgotten."

Trump intervened to reverse sentences against all three service members, ignoring Pentagon leaders who had told him such a move could damage the integrity of the military judicial system, the ability of military commanders to ensure good order and discipline, and the confidence of US allies and partners who host US troops.

Trump made the intervention on the day the House launched its public impeachment trial.

In the days afterward, the tensions between the Pentagon and the White House became concentrated on Gallagher, whose cause had been taken up by Fox News' personalities.

Spencer had indicated publicly that he felt the review should proceed, telling the audience at the Halifax Security Forum in Nova Scotia, Canada, that "the process matters for good order and discipline."

Behind the scenes, he proposed to the White House that Gallagher's review go ahead, but offered a secret guarantee that Gallagher would be allowed to keep his status as a Navy SEAL, according to the senior defense official.

The move prompted Esper's decision to ask for Spencer's resignation, according to Pentagon spokesman Jonathan Hoffman. The defense official said that Spencer's decision to circumvent his chain of command -- namely Esper -- and go straight to the White House was a violation of military policy.

Moreover, Spencer's private request to restore Gallagher's rank and allow him to retire with his Trident pin contradicted his public position, according to the statement. The Trident Pin, which is worn by Navy SEALs, is awarded following their completion of an intense qualification course and symbolizes membership of the elite military community.

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

Esper told reporters Monday that Trump ordered him to allow Gallagher to retain his Trident pin.

The Washington Post first reported Esper asking for Spencer's resignation.

'The rule of law is what sets us apart'

In his letter, Spencer makes clear his deep discomfort with Trump's decisions, which many in the Pentagon have told CNN runs directly counter to the discipline that makes the US military an elite force. "The rule of law is what sets us apart from our adversaries," Spencer wrote, calling the issue of discipline "deadly serious business."

"Good order and discipline is what has enabled our victory against foreign tyranny time and again," Spencer said, adding that the lives of US service members "quite literally depend on the professional execution of our many missions."

Current and former military officials say discipline is central to the US military ethos: that US forces are highly trained to operate in a legal and disciplined manner and if they are found guilty of violations, they must face punishment.

Beyond the impact on the military judicial process, there "could be an impact on military leaders and their ability to enact measures of good order and discipline. There also could be a potential crisis of confidence in the potential countries we're operating in," said CNN military and diplomatic analyst John Kirby, a retired admiral who has served as both Pentagon and State Department spokesman.

One reason US troops are as welcome as they are worldwide is because hosting nations "know the American military administers itself according to a very strict code of justice and we have a very good record of holding those troops accountable," Kirby said, even for minor scrapes such as "drunken driving overseas or getting into a fistfight in a bar."

The senior defense official said Esper first learned of Spencer's conversations with the White House on Friday, but that Spencer's proposal was never approved.

When Spencer then made his remarks over the weekend at the Halifax International Security Forum, Esper felt "his public statements did not match up" with what he had been doing privately, according to the official.

The New York Times reported Saturday that Spencer and Rear Adm. Collin Green, the admiral overseeing the SEALs, threatened to resign over the possible intervention by Trump. Spencer denied later Saturday that either he or Green made any such threat.

Esper has decided Gallagher would now keep his status because he "has little confidence that Gallagher would get a fair shake now from the Navy," the official told CNN. Gallagher is now expected to retire from the Navy on November 30.

UPDATE: This developing story has been updated with additional reporting.