Speaking to a crowd of 3,000 service members in a hangar at Bagram air base, Obama paid tribute to the work they and their predecessors have done at a time when his administration is planning a withdrawal from the region.
"Everybody knows Afghanistan is still a very dangerous place," Obama said. "But just look at the progress you have made possible — Afghans reclaiming their communities, girls going to schools, increases in life expectancy."
"That progress is because of you and the more than half a million Americans in the military who have served here in Afghanistan," he said. "More Afghans have hope in their future and so much of that is because of you."
Obama's visit, to mark the Memorial Day weekend, came at a delicate moment in the timeline for U.S. troops in Afghanistan. His administration is reviewing the pace of troop withdrawals from the country as it winds down U.S. participation in the regional struggle against terrorists.
But with Afghan President Hamid Karzai unwilling to sign a bilateral security agreement that would protect the rights of Americans who remain in the country after the 2014 withdrawal, Obama is considering a drawdown more speedy and complete than some of his military advisors want.
On the way to Bagram on Sunday, deputy national security advisor Ben Rhodes told reporters that Obama has not yet made a decision about the size of a force to leave in place after the withdrawal this year.
In preparation for that decision, the president wanted to hear from the commander on the ground and from U.S. diplomats in Afghanistan, in addition to the many advisors he is consulting back in Washington.
"It is important for him to come" to Afghanistan, Rhodes said, "before he articulates a decision."
Obama did not meet with Karzai, with whom he has frosty relations, nor did he meet with the two remaining candidates to replace Karzai as president. He was on the ground for about four hours.
Karzai's office issued a statement saying the Afghan president had been invited to meet with Obama at Bagram, but declined. The White House said Obama had chosen not to meet with Karzai.
The visit came amid a political uproar over complaints that Veterans Affairs facilities have concealed long waits for healthcare during the Obama administration.
Obama declared last week that he will not tolerate misconduct at the Department of Veterans Affairs, but he is standing by VA Secretary Eric K. Shinseki pending a review of the full scope of the problem. Shinseki's fate is probably tied to the outcome of a preliminary report due to the president this week.
In the meantime, though, several Republicans and at least two Democrats have called for Shinseki's resignation. Claims that VA employees kept fake waiting lists have already become an election-year issue, and lawmakers are intent on holding the president accountable.
During his visit Sunday, Obama met with wounded service members at a hospital on the base. Obama assured troops that he considers it a "sacred obligation" to take care of wounded warriors and veterans.
Air Force One touched down at Bagram air base in the dark Sunday evening after secretly leaving the Washington area Saturday night.
Obama was briefed by military commanders, telling them that the current "process of transition" is possible only because of the success of their mission.
"We've seen an election in Afghanistan, we've seen the first go successfully," Obama said. "And I'm going to have to make some decisions in conjunction with the folks on the ground about how we manage the transition not just through the end of this year but post-2014."
Obama said that he plans to announce "some decisions fairly shortly," but that he wanted to "check in directly with folks face-to-face before those decisions were finally made."
Later, Obama arrived at a hangar where thousands of troops had been entertained by country music star Brad Paisley, who flew to Afghanistan with Obama and called the event "the honor of his life."
Troops held cellphones aloft to film Obama and Paisley, as military staffers distributed water to the crowd. Paisley jokingly asked for the air conditioning to be turned on.
Standing before an enormous American flag, Obama, wearing a leather bomber jacket, got a roar from the crowd as he was introduced.
"We said that we were going to strengthen the capacity of Afghan forces so they could take more responsibility for their own security," Obama told them. "So you've been training Afghan forces and building Afghan forces up. And we know they've still got a long way to go.
"But for nearly a year, Afghans have been in the lead, and they're making enormous sacrifices," the president continued. "You look at the casualties they're taking on. They are willing to fight. Afghan forces are growing stronger. Afghans are proud to be defending their own country — and, again, so much of that is because of you."
The trip marked Obama's fourth visit to the war zone. He had declared Iraq a "dumb war" as he rose to national prominence, but presided over a buildup in forces once in office in an effort to defeat Al Qaeda and eradicate its safe havens.
Special correspondent Hashmat Baktash in Kabul, Afghanistan, contributed to this report.