WASHINGTON - Defense Secretary Jim Mattis resigned from the Trump administration Thursday, saying the president deserved someone atop the Pentagon who is "better aligned" with his views.

The retired Marine Corps general’s surprise resignation came a day after President Trump overruled his advisers, including Mattis, and shocked American allies by announcing he would be withdrawing American troops from Syria. Trump declared victory over the Islamic State, even though the Pentagon and State Department for months have been saying the fight against the group in Syria isn’t over.

The discord caused Trump to lose a Cabinet official who won widespread praise at home and abroad but who experienced increasing differences with the commander in chief he served as Trump's presidency progressed.

Mattis said he would depart the administration Feb. 28 to provide time for a replacement to be identified and confirmed by the Senate.

Mattis pointed to some of those differences in a resignation letter he submitted to the White House on Thursday. The retired general emphasized that the United States derives its strength from its relationships with allies and should treat them with respect. He said the country must also be "clear-eyed" about threats including from groups such as the Islamic State.

"My views on treating allies with respect and also being clear-eyed about both malign actors and strategic competitors are strongly held and informed by over four decades of immersion in these issues," Mattis wrote. "We must do everything possible to advance an international order that is most conducive to our security, prosperity and values, and we are strengthened in this effort by the solidarity of our alliances."

The Pentagon released the letter moments after Trump announced on Twitter that Mattis would be leaving, saying the retired general would "retire." Trump's tweet made no mention of the fact that the Pentagon chief was leaving over his differences with the president.

"General Jim Mattis will be retiring, with distinction, at the end of February, after having served my Administration as Secretary of Defense for the past two years," Trump wrote on Twitter. "During Jim's tenure, tremendous progress has been made, especially with respect to the purchase of new fighting equipment. General Mattis was a great help to me in getting allies and other countries to pay their share of military obligations. A new Secretary of Defense will be named shortly. I greatly thank Jim for his service!"

Mattis' departure adds to new uncertainty about which course the administration might take on its global challenges, including with Iran and North Korea, amid questions about the pending withdrawal of U.S. troops from Syria and a possible drawdown in Afghanistan.

The retired Marine general, 68, had repeatedly moved to reassure allies unnerved by Trump’s unpredictable pronouncements and argued successfully for continued U.S. commitments in Syria, Afghanistan, and other places where military leaders see an ongoing threat.

Most of the contenders floated to replace Mattis in the past trashed the president's decision on withdrawal from Syria this week. Retired Gen. Jack Keane called the move a "strategic mistake" on Twitter. Republican Sens. Lindsey Graham of South Carolina and Tom Cotton of Arkansas signed a letter demanding Trump reconsider the decision and warning that the withdrawal bolsters Iran and Russia.

Regardless of who is chosen, it will be hard to replace the stature Mattis attained in world affairs.

Mattis' opinion carried weight in White House deliberations, bolstered by his record as a combat leader and, at least initially, his ability to navigate the president's predilections.

With a career shaped by the wars following the Sept. 11, 2001, attacks, Mattis caught Trump's eye shortly after the presidential election in 2016. Revealing his choice for Pentagon chief, Trump hailed the retired general as "Mad Dog," a nickname Mattis earned for his conduct in battle.

On top of his field credentials, Mattis was known as an Iran hawk and a critic of the Obama administration, which had forced him out of his job at U.S. Central Command in 2013.

Although the selection of a recently retired general for a top civilian position raised some concerns among lawmakers, the decision was widely heralded as a sign that establishment figures would help guide an inexperienced president.

Taking the reins at the Pentagon, Mattis enjoyed more autonomy than his predecessors did under President Barack Obama. Trump's decision to give uniformed leaders greater control of battlefield decisions was a welcome change across the armed forces.

Mattis has overseen a surging budget in line with the president's promise to renew U.S. military primacy, allowing him to employ vast resources as he has sought to finish off terrorism threats in the Middle East and improve America's ability to compete with Russia and China.

In the early days of the Trump presidency, Mattis' partnership with Secretary of State Rex Tillerson cemented his influence within the Cabinet as he advocated staying the course in wars that the president has questioned the value of fighting.

Pentagon officials downplayed accounts of friction between Mattis and the White House as speculation grew that Mattis would join other national security leaders including Tillerson and Lt. Gen. H.R. McMaster, Trump's former national security adviser, who left their posts abruptly amid reports of distance from the president.

Avoiding the spotlight and telegraphing deference, Mattis managed to contradict the president's bombast and threats without drawing his ire. The bookish history buff quietly emphasized traditional American values even as Trump publicly questioned them.

But questions about Mattis' influence grew as the president made decisions that deviated from his advice, such as pulling out of the nuclear deal with Iran, relocating the U.S. Embassy in Israel to Jerusalem, and launching a new Space Force that many military leaders saw as unnecessary and distracting.

More recently, Trump ordered Mattis to send active duty troops to the U.S. border with Mexico, a move Democrats criticized as a political stunt that undermined the defense secretary's goal of making the military more effective in responding to foreign threats.

In recent months, media accounts described the secretary quietly challenging or setting aside requests from the White House, even from the president himself, further fueling speculation about a possible departure. In October, Trump described Mattis as “sort of a Democrat.”