Elections are underway to replace Karzai, the only president Afghanistan has known since the US-led invasion in 2001.
After an overnight flight from Washington, Obama attended a briefing with US 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.