Trump ordered Gallagher be allowed to retire as Navy SEAL, defense secretary says Navy Secretary Richard Spencer was fired over the handling of the SEAL's case.

Defense Secretary Mark Esper revealed on Monday that it was President Donald Trump who ultimately ordered Eddie Gallagher be allowed to retire as a Navy SEAL. Esper also defended his decision to fire Navy Secretary Richard Spencer over his handling of the war crimes case, saying that he was "flabbergasted" to learn Spencer, in his view, had undermined his own conversations with the president and violated the chain of command.

A Pentagon statement released on Sunday said that Esper had determined that Gallagher should retire as a Navy SEAL. But speaking to reporters on Monday, Esper said that it was actually the president who directed him to let Gallagher keep his Trident pin, which signifies his membership in the elite SEAL community.

That decision followed a long sequence of events in which the president continually worked to intervene on Gallagher's behalf over the last couple of weeks. The Navy SEAL, while acquitted of killing a wounded Islamic State captive earlier this year, was sentenced to four months of time served and a reduction in rank for posing with a corpse during a 2017 deployment to Iraq. Earlier this month, Trump restored Gallagher's rank to E-7 (Chief Petty Officer) so that he could retire with full benefits.

The Navy then decided to convene a Trident review board to determine whether Gallagher could keep his Trident pin. While the president initially tweeted on Thursday that the Navy "will NOT be taking away" the pin, the White House ultimately told the Navy that review board could go forward, two senior Navy officials told ABC News.

However, Esper said that during this time his Navy secretary was trying to cut a backroom deal with the White House that would have allowed Gallagher to retire with the Trident -- a deal that he said he learned about only on Friday following a conversation with Trump at the White House.

The proposal was "completely contrary to what we agreed to," Esper said on Monday.

That alleged breach of trust led to the defense secretary's decision to ask for Spencer's resignation, a decision which the president agreed with, Esper said.

But in his letter to Trump acknowledging his termination, a copy of which was obtained by ABC News, Spencer laid out a different narrative that suggests he does not support the idea of side-stepping a review board.

"I have strived to ensure our proceedings are fair, transparent and consistent, from the newest recruit to the Flag and General Officer level," Spencer wrote, adding, "Unfortunately it has become apparent that in this respect, I no longer share the same understanding with the Commander in Chief who appointed me, in regards to the key principle of good order and discipline."

"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 an defend the Constitution of the United States. The President deserves and should expect a Secretary of the Navy who is aligned with his vision for the future of our force generation and sustainment," Spencer wrote.

Esper conceded that it doesn't "make sense" why Spencer would try to cut a deal with the White House that would have allowed Gallagher to keep the Trident pin, while at the same time taking a moral stand in his letter of resignation.

"I can't reconcile public statements with private statements with the written word," Esper said.

In the end, Esper defended Trump's directive to have Gallagher retire as a SEAL, saying the case had been going on for months and "must end." He said that while he believes in the military justice process, the Trident review board would have placed Gallagher's fate in the hands of three non-commissioned officers who would have then been criticized for their decision, no matter what it was.

Esper said he wants the Navy and the SEALs to "move beyond this now."

"They wanted to take his pin away, and I said, "No, you're not going to take it away.” Trump told reporters on Monday. "He was a great fighter."

"He was one of the ultimate fighters, tough guy," said Trump of Gallagher. "These are not weak people. These are tough people. And we're gonna protect our war fighters."

"We're going to protect our warfighters," said Trump. "And I've been given a lot thank-yous. "

Trump said that included from some of the members of Delta Force who carried out the raid targeting ISIS leader Abu Bakr al-Baghdadi.

"They were very thankful," said Trump. "Somebody has their back, and it's called the president of the U.S., OK? We've got their back."

Trump tweeted Sunday night, "I was not pleased with the way that Navy Seal Eddie Gallagher's trial was handled by the Navy. He was treated very badly but, despite this, was completely exonerated on all major charges. I then restored Eddie's rank. Likewise, large cost overruns from past administration's contracting procedures were not addressed to my satisfaction. Therefore, Secretary of the Navy Richard Spencer's services have been terminated by Secretary of Defense Mark Esper. I thank Richard for his service & commitment. Eddie will retire peacefully with all of the honors that he has earned, including his Trident Pin."

Gallagher thanked Trump for his actions in a statement provided by his lawyer, first reported by Fox News.

"President Donald Trump you have my deepest gratitude and thanks," he said on Sunday in the statement. "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."