But he also called on Karzai to "continue to make progress" on good governance, the fight against corruption and the rule of law.
"All of these things end up resulting in an Afghanistan that is more prosperous and more secure," he said.
The White House insisted that Karzai's cabinet participate in most of the meetings with Obama.
Karzai thanked Obama for US support, said he hopes the partnership would continue between the two countries and promised that the country "would move forward into the future".
"We had a good discussion of the issues between our two countries, about the region, and of continued struggle against extremism and terrorism," he said of the meeting.
The White House said later that Karzai would visit Washington on May 12 for more talks.
Al Jazeera's David Chater, reporting from Kandahar in southern Afghanistan, said Obama's visit was aimed at discussing several issues, including pledges by Karzai to combat corruption.
"First and foremost was [determining] exactly what effort is being made by President Karzai - as he promised on his second inauguration last year - to make sure that corruption is being fought," he said.
"The next issue was to find out how the reconciliation and reintegration programme - reaching out to the Taliban - is going.
"Also, [Obama was] to find out how the fight against the drug-growing industry here in Afghanistan is going. And of course, the corruption that comes with it."
On his arrival, the US president had been greeted by General Stanley McChrystal, the commander of US and Nato forces in Afghanistan, and Karl Eikenberry, the US ambassador.
Obama later spoke to US troops at Bagram, where he thanked them for their "tremendous sacrifices" and reiterated the country's broad objectives for the mission in Afghanistan.
"Al-Qaeda and their extremist allies are a threat to the people of Afghanistan and a threat to the people of America, but they're also a threat to people all around the world," he said.
"My main job here today is to say thank you on behalf of the entire American people. You are part of the finest military in the history of the world. And we are proud of you.
"I'm confident all of you here are going to get the job done in Afghanistan."
Obama left the country shortly afterwards, heading back to the US.
His trip was his second stop in a war zone as commander-in-chief, coming about a year after a similarly secretive trip to Iraq.
There are about 120,000 foreign troops in Afghanistan and that is set to rise to nearly 150,000 by the end of this year as the US sends in more troops as part of a new strategy to try and quell the mounting violence.
The US force in the country will have risen from about 34,000 personnel when Obama took office last year to 100,000 when the deployment is completed by the summer.
Elusive pullout date
Al Jazeera's John Terrett, reporting from Washington, DC, said the point of the visit was to rally the troops.
"There was a difference in tone between the way Obama spoke to the troops just now and the way he did in New York, when he made the announcement of the extra troops, back in December.
|Obama, left, told troops that he was 'confident' that they would be successful in Afghanistan [Reuters]
"Then, one of the key points was this July 2011 withdrawal date. What we heard today was a very different kind of speech.
"He stressed on several occasions the sacrifice of the US troops and how if he didn't think this was strategically important for the US he would bring them all home today.
"I think he was preparing the ground for the fact that July 2011 will come and go and not many troops will begin to come home. I think the realisation here in the US is that the country is in for the long haul in Afghanistan."
But Obama's visit was also aimed at encouraging Karzai to take ownership of the war in Afghanistan, Terrety said.
"After all, President Obama has taken ownership of the war in Afghanistan by sending more troops," he said.
"The White House feels that that has not really happened as far as President Karzai is concerned, and they want to stress that he take more of a commander-in-chief role in the future."
An opinion poll published in the Washington Post newspaper as Obama was in Afghanistan showed that 53 per cent of Americans approve of the US president's handling of the situation in Afghanistan