President Obama addressed troops in Afghanistan on Sunday, thanking them for their service and pledging to keep a "limited" American force there after 2014 during a surprise trip from the White House to Bagram Airfield overnight.

"I'm here on a single mission. That is to thank you for your extraordinary service. I thank you as your commander-in-chief because you inspire me," Obama said. "Your willingness to step forward and serve in a time of war and say, 'Send me,' is the reason the United States stays strong and free."

Stressing security and civil-society gains Afghanistan has made, Obama congratulated troops on the U.S. mission there-a mission that's about to end, as Obama and the U.S. military have planned a drawdown of troops from Afghanistan in 2014.

"You are completing the mission. We said we were going to deny al Qaeda a safe haven," Obama said. "You've helped prevent attacks and save American lives back home. Al Qaeda is on its heels in this part of the world, and that's because of you."

The trip came as Obama is weighing America's involvement in Afghanistan after 2014-and amid some strained relations with Afghan President Hamid Karzai, who would not sign an agreement allowing U.S. troops to stay in the country. It is expected that his successor as president will sign it, leaving open the possibility for the U.S. to keep some troops in Afghanistan to assist Afghan forces.

The president gave a brief description of America's role in Afghanistan after 2014, assuming Afghanistan's next president signs the agreement to let U.S. troops remain.

"With that bilateral security agreement, assuming it's signed, we can plan for a limited security presence in Afghanistan beyond 2014," Obama said. "We want to preserve the gains that you have helped to win."

Pledging that Afghanistan will "never again, ever be used to launch an attack against our country," Obama said that "even as our combat mission ends later this year … America's commitment to the people of Afghanistan will endure."

Before speaking to troops, President Obama met with his top two officials in the country, Gen. Joseph Dunford, who commands U.S. and coalition troops in Afghanistan, and U.S. Ambassador to Afghanistan James Cunningham.

Under cover of night and without making his plans public, the president departed in Air Force One about 10:30 p.m. Saturday, flying along with National Security advisor Susan Rice, senior White House advisor John Podesta, senior White House advisor Director Dan Pfeiffer, National Security Council spokesman Ben Rhodes and country music singer Brad Paisley.

It was the president's fourth trip to Afghanistan. He first went in May 2012, visiting the Presidential Palace and delivering a televised address.

Before Obama spoke on Sunday, Paisley performed for troops stationed at the base.

While in Afghanistan, Obama offered to meet Afghan President Hamid Karzai at Bagram, but the Afghan president declined, the White House confirmed.

Karzai rejected the offer after Obama attempted to set up a meeting, saying he would meet Obama at his palace but not an American military base, according to Karzai's chief of staff.

"[W]e weren't planning for a bilateral meeting with President Karzai or a trip to the palace, as this trip is focused on thanking our troops. We did offer him the opportunity to come to Bagram, but we're not surprised that it didn't work on short notice," a White House official said. "The President will likely be speaking by phone with President Karzai in the days to come, and also looks forward to working with Afghanistan's next President after the election is complete."

Obama could soon reveal more about his plans for U.S. involvement in Afghanistan. Aboard the Air Force One flight to Bagram, the NSC spokesman Ben Rhodes told reporters that Americans can expect "additional clarity" about Obama's thinking on Afghanistan in the coming days, noting the president's scheduled commencement address at West Point on Wednesday and an upcoming NATO defense ministerial meeting on June 4.

"We've been looking for some period of time now, I'd say a few months, to come to Afghanistan precisely because we wanted to be able to thank the troops. … After the first round of the election went off well, we felt that there would be a good window to come on a troop-focused visit," Rhodes said, calling the trip "an opportunity for the president to thank American troops and civilians for their service."

Afghanistan will hold its runoff election June 14 to decide its new president.

ABC's Robin Sproul contributed to this report.

This post has been updated to reflect Dan Pfeiffer's correct title. He is a senior White House advisor, not communications director.