Trump visited wounded USA troops last Christmas season, on December 21, 2017, at Walter Reed National Military Medical Center in Maryland.
Mattis was supposed to continue leading the Pentagon for several weeks, but Trump moved up his exit and announced that Patrick Shanahan, deputy defence secretary, would take the job on January 1 and he was in "no rush" to nominate a new defence chief.
It is unclear whether his trip to Iraq was added after it became apparent that the government would be shut down indefinitely due to a stalemate between Mr Trump and congressional Democrats over the president's demand for a wall along the US-Mexico border.
Last week, Trump said that he would withdraw all U.S. soldiers in Syria, a decision that was met with shock within the U.S. government as well as regional allies.
On his stop in Iraq, he defended his decision to pull out the 2,000 troops from Syria, which he has said was made possible by the defeat of ISIS militants. The visit, which was not announced in advance, came against the backdrop of mounting controversies over Trump's plans to withdraw forces from the Middle East, the ongoing government shutdown, and an apparently coincidental New York Times article that explored the president's past avoidance of military service.
Asked about United States troops in Iraq, a little over 5,000, the president said, unequivocally, he had no plans of pulling them out yet.
Although Trump has previously addressed USA troops stationed overseas, including in Italy, Japan and South Korea, he has drawn criticism for not yet visiting those deployed to combat zones.
The Trump administration's withdrawal plans, which U.S. military commanders on the ground in Afghanistan have not received yet, have caused worries in the region.
US President Donald Trump speaks to members of the US military during an unannounced trip to Al Asad Air Base in Iraq
New Delhi, with billions of dollars in developmental investment, also banks on the US-led global coalition forces for much of its reconstruction effort in the country, which started years ago on the watch of president George W Bush and continue through president Barack Obama's tenure to president Trump. "The American leadership was defeated in Iraq and wants to return again under any pretext, and this is what we will never allow", he said.
The decision to pull US forces from Syria, however, stunned national security advisers and USA allies and prompted the resignations of Defence Secretary Jim Mattis, who was not on the trip, and the US envoy to the coalition fighting the Islamic extremist group.
"So that's the way it is", Trump said, according to a White House transcript.
There are some 5,000 U.S. soldiers in Iraq, compared to about 2,000 on the ground in Syria.
Despite the wide grins and supportive cheers from his audience, the trip comes at a time of growing turmoil for the president at home and overseas. "If we see something happening with ISIS that we don't like, we can hit them so fast and so hard" that they "really won't know what the hell happened".
Iraqi lawmakers told Reuters that the pair had disagreed over where their planned meeting should take place: Trump had asked to meet at the Ain al-Asad military base, an offer which Abdul Mahdi declined.
The US has kept a sustained military presence in Iraq since the 2003 invasion launched by former President George W Bush. It is his first visit with troops stationed in a troubled region.
Trump has described the United States involvement in Afghanistan as a "complete waste" and vowed to bring home American troops.