In his first major official act since losing re-election, Mr Trump said Mr Esper would be replaced by Christopher Miller, a former army officer who now serves as a senior intelligence official.
The decision was announced on Twitter, where the president wrote Mr Esper “has been terminated” and that Mr Miller would take over as acting secretary “effective immediately”.
Mr Trump fell out with his defence secretary in June after Mr Esper said he opposed invoking the Insurrection Act to send US troops into American streets to quell civil unrest, except as a last resort, as anti-racism protests were sweeping several US cities.
Mr Trump had publicly mocked Mr Esper as “Yesper”, a nickname from colleagues who had previously seen him as a yes-man who failed to stand up to Mr Trump.
Mr Esper also supported a bipartisan congressional effort to redesignate southern military bases that bear the names of confederate civil war officers, some of whom owned slaves. Mr Trump vowed to veto the annual defence spending bill if it contained such provisions.
Resignations are common for high-ranking political appointees of outgoing administrations, but the announcement on Twitter of Mr Esper’s firing was especially unusual. Mr Trump has not yet conceded defeat in the presidential election, and there are still two months before the Biden administration takes office.
“Firing a competent defence secretary with two months remaining in his term is exactly the kind of petty recklessness that made so much of the Republican defence establishment support Joe Biden for president,” said Kori Schake, a foreign policy and defence expert at the conservative American Enterprise Institute.
Since the election, Mr Esper has appeared to distance himself from Mr Trump, telling an interviewer in the independent Military Times newspaper that he had been trying to “make the best out of it” and denied fawning over the president.
“Hmm, 18 Cabinet members. Who’s pushed back more than anybody? Name another Cabinet secretary that’s pushed back,” Mr Esper said in the post-election interview, which was published shortly after Mr Trump announced his firing. “Have you seen me on a stage saying, ‘Under the exceptional leadership of blah-blah-blah, we have blah-blah-blah-blah’?”
Mr Trump had originally described Mr Esper, a former Raytheon defence lobbyist, as “a tremendous talent” who would do very well in the Pentagon post.
Mr Esper’s dismissal comes amid reports that Mr Trump could discharge other members of his national security team, a group that has already seen unprecedented turnover during his first term.
James Stavridis, a retired admiral and former US commander of European and Southern forces, argued the move “makes no sense at this point”.
“Things are already unstable internationally, and this does not help,” he wrote on Twitter, adding he hoped opponents would not seek to take advantage.
Adam Smith, Democratic chairman of the House armed services committee, criticised Mr Trump’s decision as “a destabilising move will only embolden our adversaries and put our country at greater risk”, adding it was a childish and reckless action made out of “spite”.
Mark Warner, the top Democrat on the Senate select committee on intelligence, said he was “deeply troubled” by Mr Esper’s dismissal.
“President Trump must not invite further volatility by removing any Senate-confirmed intelligence or national security officials during his time left in office,” he said in a statement.
The FT ViewThe editorial board
Biden turns the page on a presidency of rancour
Jack Reed, the top Democrat on the Senate armed services committee who is among the people Mr Biden may consider appointing as his own defence secretary, described the move as “a play for attention”.
US adversaries were “already seeking vulnerabilities they can exploit in order to undermine American global leadership and national security during this transition period”, he added.
Mr Miller a former Green Beret, currently serves as director of the national counter-terrorism centre, a unit within the US intelligence community. He is a veteran of the conflicts in Afghanistan and Iraq.
During Mr Trump’s tenure, he worked first as a counter-terrorism official at the National Security Council and then as a senior official for special operations at the Pentagon. Mr Miller was sworn in to his latest role in August amid a controversial shake-up at the office of the director of national intelligence.
One former colleague in the intelligence community described Mr Miller as “quirky” and “a good guy with a great temperament”. Another described him as “a professional” and “a known quantity”.
The Pentagon declined to comment and referred inquiries to the White House.