"Gen. 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. Trump also noted that he was grateful for Mattis' time in his administration and would announce his successor "shortly," although he didn't specify when.

Trump largely praised Mattis in his tweets. However, Mattis' letter to the president, released right after Trump tweeted, referred to his departure as a resignation. "Because you have the right to have a Secretary of Defense whose views are better aligned with yours on these and other subjects, I believe it is right for me to step down from my position," Mattis wrote. He also notably did not thank the president in his letter.

In his resignation letter, Mattis also wrote, "One core belief I have always held is that our strength as a nation is inextricably linked to the strength of our unique and comprehensive system of alliances and partnerships."

"While the U.S. remains the indispensable nation in the free world," Mattis added, "we cannot protect our interests or serve that role effectively without maintaining strong alliances and showing respect to those allies."

While Mattis did not specify which allies Trump had insulted in the past, he may have been referring to Trump hurling insults at Germany at the NATO annual summit in July. That summer, Trump called Germany a "captive of Russia" and that it was "making Russia richer."

It was at that controversial diplomatic meeting that a terse NATO Secretary General Jens Stoltenberg said, "In the history of NATO we have had many disagreements and we have been able to overcome them again and again."

Chip Somodevilla/Getty Images News/Getty Images

In spite of their differences, Mattis wrote in his resignation letter that he would ensure that a transition took place sans trouble.

"I pledge my full effort to a smooth transition that ensures the needs and interests of the 2.15 million Service Members and 732,079 Department of Defense civilians receive undistracted attention of the Department at all times so that they can fulfill their critical, round-the-clock mission to protect the American people," he wrote.