While many of the 32,800 U.S. forces now in Afghanistan will leave in the coming months, Obama said a continued military presence could help protect gains made during nearly 13 years of fighting.
"After all the sacrifices we've made, we want to preserve the gains you have helped to win," Obama said.
Obama appeared optimistic that the Afghan government soon would sign a bilateral security agreement allowing the U.S. to keep some forces in the country to train Afghans and launch counterterrorism operations.
He has been considering keeping up to 10,000 troops in Afghanistan and said he would announce his plans shortly.
That announcement could come as early as Wednesday, when Obama delivers the commencement address at the U.S. Military Academy at West Point, New York.
Obama secretly arrived at Bagram Air Field, the main U.S. base in Afghanistan, under the cover of darkness for his first trip to the war zone since 2012. He was spending just a few hours on the base and had no plans to go to Kabul, the capital, to meet with Hamid Karzai, the mercurial president who has had a tumultuous relationship with the White House.
Obama's visit, his fourth to Afghanistan as president, came at a time of transition for a country long mired in conflict. Most of the U.S. and international forces in Afghanistan are withdrawing ahead of year-end deadline. Elections are underway to replace Karzai, the only president Afghanistan has known since the U.S.-led invasion in 2001.
Karzai stunned the White House by refusing to sign a bilateral security agreement needed to keep U.S. troops in Afghanistan after this year. But both candidates on the ballot in next month's Afghan presidential election runoff have said they plan to quickly approve the accord.
At least 2,181 members of the U.S. military have died during the nearly 13-year Afghan war and thousands more have been wounded.
After an overnight flight from Washington, Obama attended a briefing with U.S. officials. He said that as he entered the briefing room, he saw a poster of the Twin Towers destroyed in the 2001 terrorist attacks.
"It's a reminder of why we're here," he said.
This was Obama's fourth visit to Afghanistan as president, but his first since winning re-election in 2012.
As is typical of recent presidential trips to war zones, the White House did not announce Obama's visit in advance. Media traveling with Obama for the 13-hour flight had to agree to keep the trip secret until the president arrived at the air base.