After Air Force One touched down under heavy security, Bush strode across the tarmac and into a giant tent where hundreds of troops greeted him with raucous cheers as he thanked them for their service.
"I am confident we will succeed in Afghanistan because our cause is just," he told them.
Bush, who has ordered a troop increase in Afghanistan, appeared to lend tacit support to a pledge by Obama to increase troop levels in Afghanistan after he takes office on January 20.
"I recognised that we needed more troops. President-elect Obama is going to make decisions on troops. And we've been calling on our Nato allies to put in more troops," Bush said.
"I am confident we will succeed in Afghanistan because our cause is just"
out-going US president
Obama has promised to make Afghanistan a higher priority, saying the Bush administration has been too distracted by the unpopular Iraq war to pay Afghanistan the attention it deserves.
Bush also said it was important for the US to keep working with Pakistan to maintain pressure on fighters along its border with Afghanistan.
"If Pakistan is a place from which people feel comfortable attacking infrastructure, citizens, troops, it's going to make it difficult to succeed in Afghanistan," Bush said.
"The more we can get Pakistan and Afghanistan to co-operate, the easier it will be to enforce that part of the border regions."
Sign of contempt
On his farewell visit to Baghdad on Sunday, meant to mark greater security in Iraq after years of bloodshed, an Iraqi reporter called Bush a "dog" and threw his shoes at him, shouting "this is the end".
Bush, who had been giving a joint press statement with Nuri al-Maliki, the Iraqi prime minister, ducked as the shoes narrowly missed his head.
He was reported to be unhurt after the attack by Muntadar al-Zeidi, a correspondent for Al-Baghdadiya television.
Bush had just said that while the war in Iraq was not over "it is decisively on its way to being won", when al-Zeidi got to his feet and hurled abuse - and his footwear - at him.
In Iraqi culture, throwing shoes at someone is a sign of contempt.
The incident will serve as a vivid reminder of the widespread opposition to the US-led invasion of, and subsequent war in, Iraq - the conflict which has come to define Bush's presidency.
Bush shrugged off the incident and quipped: "All I can report is that it's a size 10."
Adil Shamoo, an Iraqi analyst at the Institute for Policy Studies in Washington DC, told Al Jazeera that "we should go beyond the shoe and think about the fact that the US should respect Iraq's sovereignty in order to regain respect of the Iraqi people and the Arab world".
"I think Bush has increased terrorism against the United States and instability in the Middle East because of his policies," he said.
Bush was in Baghdad to sign a pact between Iraq and Washington that will allow American troops to stay in Iraq until the end of 2011.
Al-Maliki applauded security gains in Iraq and said that two years ago "such an agreement seemed impossible".