President Obama thanked troops at Bagram Air Base for their service Sunday as part of a surprise visit to Afghanistan. (Reuters)

President Obama arrived in Afghanistan on Sunday for an ­unannounced visit to mark ­Memorial Day with U.S. troops, now in the final months of America’s longest war, and to begin final discussions over the size of the U.S. force that will remain beyond the end of the year.

Inside a cavernous hangar with a tennis-court-size American flag as a backdrop, Obama told a raucous audience of about 3,000 U.S. troops that the American public “stands in awe of you,” grateful for their service and united in support of veterans as they return home.

“For many of you, this will be your last tour in Afghanistan,” Obama said to roars, adding that at the end of the year, “America’s war in Afghanistan will come to a responsible end.”

“That progress is because of you,” he said, “and the more than half a million Americans — military and civilian — who’ve served here in Afghanistan.”

Obama departed Washington on Saturday night under cover of darkness and arrived at this U.S. base outside Kabul, the capital, under the same secrecy. It was his fourth trip to Afghanistan as president and his first in two years.

The visit lasted less than four hours. But it came at a crossroads moment for Afghanistan’s political transition as the long tenure of President Hamid Karzai winds down and for the Obama administration’s postwar strategy, which advisers say he will begin describing publicly in the coming weeks.

Obama met first with Gen. ­Joseph F. Dunford Jr., commander of U.S. and international forces in Afghanistan, and Ambassador James Cunningham to receive a battlefield update and discuss the civilian and military resources needed here after this year to continue training Afghan forces and to assist in specific counterterrorism missions.

Obama will begin outlining those plans Wednesday in a speech at the U.S. Military Academy at West Point, N.Y., where he intends to trace the broader shift underway, more than a decade after the Sept. 11, 2001, attacks, from an American wartime foreign policy to a postwar one.

“We are at a bit of a turning point in our foreign policy generally,” Ben Rhodes, deputy national security adviser for strategic ­communications, told reporters aboard Air Force One. “Our foreign policy is going to be a lot different than it has been over the past decade, and the president will speak to what that transition will mean.”

A transition is also underway in Afghanistan, which is in the midst of the first democratic transfer of power in its long history.

Obama did not meet with Karzai, with whom he has had a stormy relationship, but they spoke by phone for about 20 minutes before Obama left Bagram. A senior administration official said Obama praised Karzai for progress being made by Afghan security forces and for his country’s successful first-round election in April and said he continued to support Afghan-led peace negotiations with the Taliban.

For the most part, Obama is biding his time until Karzai’s ­tenure, which has spanned the post-9/11 period, ends this summer. The country’s presidential election in April produced two finalists — former Afghan foreign minister Abdullah Abdullah and onetime World Bank economist Ashraf Ghani — who are scheduled for a runoff June 14. Administration officials said before the visit began that Obama would not inject himself into the runoff campaign by meeting with either candidate or discussing the race.

“We are mindful that we are in the middle of an election season,” Rhodes said.

But he added that Obama’s visit was meant, in part, to assure the Afghan public that “no matter what happens, we have an enduring commitment to Afghanistan.”

Obama and his military command are eager for the election to be resolved. The winner will be asked immediately to sign a security agreement that will help determine how many U.S. forces, now numbering 32,000, will remain in Afghanistan after the end of the year. The number could range as high as 10,000 troops to meet what Rhodes said would be a twin training and counterterrorism mission.

Karzai confounded Obama last year by refusing to sign the agreement after months of negotiation, saying that such a significant step should be left to his successor. Both Abdullah and Ghani have stated publicly that they intend to sign within days of taking office, probably in July.

A Karzai spokeswoman, Adela Raz, said that the U.S. Embassy tried to arrange a meeting during Obama’s visit to Bagram but that the Afghan president declined. Raz said Karzai invited Obama to the presidential palace instead. Afghan officials indicated that asking Karzai to meet at the U.S. military base, rather than his own home, was a sign of disrespect.

A bilateral meeting was not planned, the White House said in a statement, because the trip was “focused on thanking our troops.”

“We did offer him the opportunity to come to Bagram,” the statement said, “but we’re not surprised that it didn’t work on short notice.”

U.S. officials say the security agreement must be endorsed as soon as possible to give U.S. military planners time to complete drawdown schedules — including decisions on what bases to close — and make arrangements for the next phase of the U.S. military presence after nearly 13 years of war.

Obama said the signing would allow planning to begin for a “limited” military presence beyond the end of the year.

“Everybody knows Afghanistan is still a very dangerous place,” he said. “But just look at the progress you have made possible — Afghans reclaiming their communities, and more girls returning to school, dramatic improvements in public health and life expectancy and literacy.”

“More Afghans have hope in their future, and so much of that is because of you.”

Obama last visited in May 2012 — a year to the day after U.S. forces killed al-Qaeda leader Osama bin Laden in neighboring Pakistan — to sign a strategic partnership agreement that began to mark out the parameters of America’s postwar relationship with Afghanistan.

In the midst of his reelection campaign, Obama used the agreement and the bin Laden anniversary to signal in a nationally televised address “a future in which war ends and a new chapter begins.” Now he has turned much of his attention to bringing those troops who remain here home by the year’s end and preparing a country where only a tiny fraction of the population fought in America’s post-9/11 conflicts to receive them.

His visit here came against the backdrop of rising anger at home among veterans advocacy groups over his management of the Department of Veterans Affairs, where a backlog in benefit payments persists and new evidence has arisen that some VA medical centers have covered up the extended wait times that many veterans are experiencing. Obama said last week that those found responsible for the problems would be held accountable.

The public centerpiece of his visit was his address to U.S. troops, who were entertained beforehand by country music star Brad Paisley. Paisley, who traveled aboard Air Force One for the visit and who has performed at the White House for veterans and their families in the past, called the event “the honor of my life.”

Since Obama took office, nearly 2,000 U.S. service members have died in Iraq and Afghanistan. More than 17,000 have been wounded, and the president saw some of them Sunday during a private visit to the Bagram military hospital. In remarks last week, Obama called such visits the “most searing moments of my presidency,” all of them taking place outside public view.

But the U.S. involvement here is receding, as shown by declining casualty figures as Afghan forces take the lead role in combat. This year, 12 U.S. service members have been killed in combat operations here, while Afghan soldiers have died at much higher rates, though the Afghan government does not release those figures.

Kevin Sieff in Kabul contributed to this report.