But on Wednesday, about 100 American servicemen and women, some of whom were wearing red “Make America Great Again” caps, greeted Mr. Trump with a standing ovation in Al Asad Air Base’s dining facility, which had been decorated for Christmas. He and Mrs. Trump spent about 15 minutes there talking with the troops.

The president told reporters that he had chosen Iraq for his first visit to a combat zone because “it’s a place that I’ve been talking about for many years.”

“And many, many years, before it started, I was talking about it, as a civilian,” he said.

Mr. Trump, who left the White House late Christmas night, said he had harbored some safety concerns about the trip.

“I had concerns for the institution of the presidency because — not for myself, personally,” he said. “I had concerns for the first lady, I will tell you. But if you would have seen what we had to go through, with the darkened plane, with all windows closed, with no lights on whatsoever, anywhere — pitch black. I’ve never seen it. I’ve been in many airplanes — all types and shapes and sizes. I’ve never seen anything like it.”

Mr. Trump ran for the presidency in 2016 on a platform of bringing the troops home from Afghanistan and Syria. It was part of a broader strategy of ending nearly two decades of American military interventions — including in Iraq, Libya, Syria and Afghanistan — that he criticized as costly, ineffective and at odds with his “America First” foreign policy.

But the United States still has 14,000 troops in Afghanistan and about 2,000 in Syria. While the number of casualties in these conflicts is a fraction of what it was during the two previous administrations, the fact that American troops are still on the ground — in the case of Afghanistan, 17 years after they were first deployed — attests to the difficulty of extracting the United States from these entanglements.

Mr. Trump, who was also accompanied to Iraq by his national security adviser, John R. Bolton, and a small group of reporters, said that “the United States cannot continue to be the policeman of the world.”