Trump landed at 7:16 pm local time at Al-Asad Air Base in western Iraq, accompanied by his wife Melania, following what he described as a stressful, secrecy shrouded flight on a "pitch black" Air Force One.
The president spoke to a group of about 100 mostly special forces personnel and separately with military leaders before leaving a few hours later. A planned meeting with Iraqi Prime Minister Adel Abdel Mahdi was scrapped and replaced by a phone call, the premier's office said.
During the call, Trump invited Abdel Mahdi to visit Washington and he accepted, White House spokeswoman Sarah Sanders said.
White House video showed a smiling Trump shaking hands with camouflage-clad personnel, signing autographs and posing for photos at the base in Iraq.
Morale-boosting presidential visits to US troops in war zones have been a longstanding tradition in the years following the September 11, 2001 terrorist attacks.
Trump has taken considerable criticism for declining to visit in the first two years of his presidency. But speculation had been mounting that he would finally make the gesture following his controversial plan to slash troop levels in Afghanistan and his order to withdraw entirely from Syria.
At the Iraqi military base, Trump sought to defend his "America First" policy of pulling back from multinational alliances, including what to many Americans seem like the endless wars of the Middle East.
"It's not fair when the burden is all on us," he said. "We don't want to be taken advantage of any more by countries that use us and use our incredible military to protect them. They don't pay for it and they're going to have to."
"We are spread out all over the world. We are in countries most people haven't even heard about. Frankly, it's ridiculous," he added.
Trump told reporters he had overruled generals asking to extend the Syria deployment, where about 2,000 US forces, joined by other foreign troops, assist local fighters battling the Islamic State jihadist group.
"You can't have any more time. You've had enough time," he said he told the top brass.
Trump's visit appeared to take Iraqi authorities by surprise and prompted criticism including from former prime minister Haider al-Abadi.
The way in which Trump visited was "not commensurate with diplomatic norms and relations with sovereign states", Abadi said.
"Dealing with Iraq and its sovereignty in this way will harm Iraqi-US relations," he said.
Iran-backed groups that make up a key bloc in the Iraqi parliament reacted angrily.
Shiite movement Harakat al-Nujaba said the presence of US forces in Iraq was a "violation of the country's sovereignty" and it was "now the duty of the government to expel American forces".
"Trump's disrespect of Iraq's sovereignty will not go unpunished," it said, adding it would "not allow Iraq to be a base used to threaten other countries".
The head of the Asaib Ahl al-Haq pro-Iran paramilitary group, Qais al-Khazali, tweeted that "Trump's visit to a US military base without regard for diplomatic norms reveals the reality of the American project in Iraq".
The Iraqi response, he said, "would be the parliament's decision to remove your military forces" unilaterally.
Trump's troop drawdowns – and the abrupt way that they were announced – helped trigger the resignation of his defence secretary, Jim Mattis, who has been one of the administration's heavyweights.
But the president has made disentangling America from its wars a priority since his 2016 election and he said in Iraq that the US would no longer be treated as "suckers."
IS, which once controlled swaths of territory in Iraq and Syria, has been driven mostly into hiding.
On Wednesday, Trump said "we've knocked them out," although he added Iraq might be used as a future base "if we wanted to do something in Syria."
According to a transcript released by the White House, he thanked troops for the "near-elimination" of IS, and said some US troops from Syria "will come here for a stay, but a lot of them are going to be going back home".
According to Trump, the flight into Iraq was unlike anything he'd previously experienced.
"If you would have seen what we had to go through in the darkened plane with all windows closed with no light anywhere – pitch black," he said.
After departing Iraq, Trump stopped over at Ramstein Air Base in Germany, before flying on to the United States, with Air Force One touching down early Thursday at Joint Base Andrews outside Washington.