Even first lady Laura Bush was not told of the daring trip until two hours before the president sneaked out of his Texas ranch, avoiding the White House press corps.
The visit, timed to coincide with the American Thanksgiving holiday, was the first by an US president to Iraq a mission tense with concern about his safety.
"You are defending the American people from danger and we are grateful," Mr Bush told 600 soldiers who were stunned and delighted by his appearance. Mr Bush flew in on Air Force One and White House officials went to extraordinary lengths to keep the trip a secret, fearing attempts to kill him.
The news of Mr Bush's flight was not released until he was in the air on the way back to the US.
"If this breaks while we're in the air we're turning around," White House communications director Dan Bartlett told reporters on the flight to Baghdad.
While in Baghdad, Mr Bush spoke with soldiers from the 1st Armoured Division and the 82nd Airborne Division at an airport mess hall.
"You are defeating the terrorists here in Iraq so we don't have to face them in our own country."
Terrorists are testing America's resolve, he said, and "they hope we will run".
"We did not charge hundreds of miles into the heart of Iraq, pay a bitter cost of casualties, defeat a ruthless dictator and liberate 25 million people only to retreat before a band of thugs and assassins," the president said, prompting a standing ovation and cheers.
Wearing a bomber jacket with a 1st Armored Division patch, Mr Bush stood in a queue for food and then dished out sweet potatoes and corn to troops who had arrived for their Thanksgiving dinner.
He posed with a platter of a fresh roasted turkey.
The troops had been told that the VIP guests would be Paul Bremer, the US administrator in Iraq.