KABUL, Afghanistan — The last time President Obama visited Afghanistan, he came to sign a strategic partnership agreement with President Hamid Karzai.

But that was two years ago. When Mr. Obama returned to Afghanistan on Sunday, conspicuously absent from the agenda was a meeting with Mr. Karzai, who has staunchly opposed an enduring American presence here.

The trip to Afghanistan was unannounced, and Mr. Obama slipped out of the White House secretly on Saturday evening. He arrived a day ahead of Memorial Day under the cover of darkness at Bagram Air Base, the sprawling American encampment north of Kabul, accompanied by the country music singer Brad Paisley, who performed for about an hour before the president spoke to troops.

Though Mr. Obama did not meet with Mr. Karzai, who is to leave office in the coming months, he vowed to work with whoever wins the presidential runoff scheduled for June 14: either Abdullah Abdullah or Ashraf Ghani.