Secretary of Defense Mark Esper said Monday he was given a direct order by President Trump to allow Eddie Gallagher to retire without losing his status as a Navy SEAL, while also saying he was “flabbergasted” by now-fired Navy Secretary Richard Spencer’s handling of the case.

“I spoke to the president on Sunday, he gave me the order that Eddie Gallagher will retain his Trident,” Esper told reporters.

EDDIE GALLAGHER CONTROVERSY: ESPER FIRES NAVY SECRETARY, SEAL WILL KEEP TRIDENT PIN, PENTAGON SAYS

Asked by a reporter if he considered that a formal order from Trump, Esper said, “Absolutely.”

Gallagher was acquitted of murder in the stabbing death of an Islamic State militant captive but convicted of posing with the corpse while in Iraq in 2017.

Last week Trump had tweeted that he wanted Gallagher to be allowed to retire with his Trident Pin, but Esper's comments Monday revealed that Trump had given the defense secretary a direct order to make this happen.

In his remarks, Esper accused Spencer of secretly offering to the White House to rig the Navy disciplinary process to ensure the Gallagher not lose his Trident. Esper fired Spencer on Sunday, saying he had lost trust in him.

“Contrary to the narrative that some have put forward in the media, this dismissal is not about Eddie Gallagher,” Esper said. “It’s about Secretary Spencer and the chain of the command.”

Esper said he was "flabbergasted" when he learned at the White House on Friday that Spencer had gone behind his back to propose the secret deal.

In his termination letter sent to Trump, Spencer said he could not in good conscience follow an order that he believed would undermine the principle of good order and discipline in the military - suggesting that he had been ordered to stop the peer-review process for Gallagher.

“Unfortunately it has become apparent that in this respect,” Spencer wrote in the letter. “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 and defend the Constitution of the United States.”

Controversy has swirled around whether or not the Navy would strip Gallagher of his Trident Pin, ousting him from the prestigious SEALs after he was demoted from chief petty officer to 1st class petty officer following his conviction in July.

Esper and Chairman of the Joint Chiefs of Staff Mark Milley spoke to Trump on Friday with the intention of persuading the president to allow the Trident review board to go forward with its inquiry.

Instead, Esper learned that Spencer previously and privately proposed to the White House – contrary to Spencer’s public position – to restore Gallagher’s rank and let him retire with his Trident Pin, the Pentagon said. When Esper recently asked, Spencer confirmed that he'd never informed the defense secretary about his private proposal.

Fox News' Jennifer Griffin and The Associated Press contributed to this report.