Trump picked Shanahan, a former Boeing executive with no previous government or military experience, to serve as acting defense secretary, pending a decision on who would be nominated to serve in a more permanent capacity.
In a written farewell message, Mattis quoted a one-sentence telegram that President Abraham Lincoln sent to the commander of Union forces, General Ulysses S. Grant, on February 1, 1865, in the final weeks of the Civil War.
It said: "Let nothing which is transpiring, change, hinder, or delay your military movements, or plans."
On that date, Lincoln signed a joint congressional resolution proposing a constitutional amendment to abolish slavery.
Mattis, who resigned over a series of policy disagreements with Trump, including over the president's decision to withdraw US troops from Syria, said he's confident Pentagon employees will remain "undistracted from our sworn mission to support and defend the Constitution while protecting our way of life.
"Our Department is proven to be at its best when the times are most difficult," he wrote. "So keep the faith in our country and hold fast, alongside our allies, aligned against our foes."
Mattis had initially said he planned to serve as head of the Defence Department through February to ensure an orderly transition. But the shock and dismay over his decision to leave annoyed Trump, who accelerated Mattis' departure to January 1.
His farewell message was just eight sentences, but it echoed some of the key themes in his lengthier resignation letter, including an emphasis on the value of having allies such as those in NATO.
This is a point on which he differed with Trump, who often denigrated allies as unworthy freeloaders.
Mattis, 68, has not publicly commented on his reasons for resigning, beyond what he wrote in his resignation letter, in which he expressed pride at having served with the men and women of the Defence Department but did not directly thank Trump. He wrote that the president has "the right to have a secretary of defence whose views are better aligned with" his.
"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.