"Our Department is proven to be at its best when the times are most difficult," Mattis said in the three-paragraph statement that hit on the theme he stressed earlier this month in his letter of resignation -- the commitment to work with allies to protect the nation's national security interests.
"So keep the faith in our country and hold fast, alongside our allies, aligned against our foes," the 68-year-old Mattis said in a farewell that follows more than 40 years in uniform as a Marine and nearly two years as Defense secretary.
"It has been my high honor to serve at your side," Mattis said. "May God hold you safe in the air, on land, and at sea."
At the White House earlier this month, Mattis handed Trump his letter of resignation a day after the president on Dec. 19 announced that the estimated 2,000 U.S. troops in Syria would be withdrawn.
In a series of Tweets before Mattis' farewell was posted Monday, Trump hit back against the retired "failed generals" who had criticized the withdrawal and questioned his fitness to be commander-in-chief.
Trump said that "I campaigned on getting out of Syria and other places. Now when I start getting out the Fake News Media, or some failed Generals who were unable to do the job before I arrived, like to complain about me & my tactics, which are working. Just doing what I said I was going to do!"
Trump did not name the "failed generals," but his Twitter posts followed the scathing criticism aimed at him Sunday by retired Army Gen. Stanley McChrystal in an interview on ABC-TV's "This Week" program Sunday.
McChrystal called Trump "immoral" and said potential successors to Mattis should consider whether they would have to compromise their own principles to serve under him.
Trump announced that Deputy Defense Secretary Patrick Shanahan, a former Boeing executive and the No. 2 at the Pentagon, would assume Mattis' duties and serve as Acting Secretary until the formal announcement of the nomination of a replacement.