President Obama wrapped up a surprise visit to Afghanistan tonight by promoting a new security agreement and pledging to wind down the decade-long war over the next two-and-a-half years.

"By the end of 2014, the Afghans will be fully responsible for the security of their country," Obama said during a nationally televised speech from Bagram Air Base near Kabul, capping a six-hour visit that had been shrouded in secrecy.

The president described the transition as "a future in which war ends, and a new chapter begins."

Obama cited the one-year anniversary of the death of Osama bin Laden, the al-Qaeda terrorist leader who planned the 9/11 attacks from a base in Afghanistan. Victory in the war on terrorism that began after 9/11 is within reach, he said: "This time of war began in Afghanistan, and this is where it will end.

"With faith in each other, and our eyes fixed on the future, let us finish the work at hand and forge a just and lasting peace," Obama concluded.

Obama said that "international troops will continue to train, advise and assist the Afghans, and fight alongside them when needed" after the 2014 turnover. "But we will shift into a support role as Afghans step forward," he said.

Security blanketed the trip that Obama took from Washington to Afghanistan under cover of night; the president delivered his speech at 4 a.m. local time.

Noting the recent end of the war in Iraq, Obama said that "here in the predawn darkness of Afghanistan, we can see the light of a new day on the horizon. ... The number of our troops in harm's way has been cut in half, and more will soon be coming home. We have a clear path to fulfill our mission in Afghanistan, while delivering justice to al-Qaeda."

Earlier, at the presidential palace in Kabul, Obama and Afghanistan President Hamid Karzai signed a 10-year "strategic partnership" that takes effect with the scheduled end of U.S. combat operations in late 2014. Obama told his Afghanistan counterpart, "Together, we're now committed to replacing war with peace."

Speaking later to U.S. troops at Bagram, Obama said the agreement "signals" the transition to Afghanistan-led security. He also warned the troops that "the battle is not yet over" in Afghanistan, and "some of your buddies may get killed" in the remaining months.

Pledging to help the troops returning home, Obama praised them for their accomplishments in a war that "came to us on 9/11," including the death of bin Laden. "You earned a special place in our hearts," he said, "and I could not be prouder to be your commander in chief."

The president also visited a hospital at the base, handing out 10 Purple Hearts to wounded soldiers.

During the signing ceremony with Karzai, Obama said the new strategic partnership -- which took 20 months to negotiate -- reaffirms the bond between Americans and Afghans after more than 10 years of war.

"Today, with the signing of the strategic partnership agreement, we look forward to a future of peace," the president said. "Today we're agreeing to be long-term partners."

The agreement takes effect at the end of 2014, the year in which the U.S. and its allies have agreed to withdraw combat forces and turn over security responsibilities to the Afghans.

Karzai said the deal assured an "equal partnership" between the United States and Afghanistan.

"Mr. President, there will be difficult days ahead," Karzai told Obama. "As we move forward, I'm confident Afghan forces will grow stronger and the Afghan people will take control of their future."

There will likely be U.S. troops in Afghanistan after 2014, but their numbers and duties have not yet been determined.

Currently, the U.S. has about 88,000 troops in Afghanistan; its coalition partners have an additional 40,000.

In his speech, Obama noted that he withdrew 10,000 U.S. troops from Afghanistan last year, and "another 23,000 will leave by the end of the summer. After that, reductions will continue at a steady pace, with more of our troops coming home."

Obama also said, "We will not build permanent bases in this country, nor will we be patrolling its cities and mountains. That will be the job of the Afghan people."

More than 1,800 U.S. troops have been killed in Afghanistan over the past 10-plus years.

"As we emerge from a decade of conflict abroad and economic crisis at home, it is time to renew America," Obama said during his speech.

The president's visit to Afghanistan came just a few weeks before G-8 and NATO summits at which the Afghanistan withdrawal will be a big topic.

It also came a year after Obama announced bin Laden's death following a U.S. military raid on the 9/11 architect's compound in neighboring Pakistan. The raid is currently an issue in Obama's re-election campaign against Republican challenger Mitt Romney.

One Republican senator -- Jim Inhofe of Oklahoma -- called the Afghanistan trip "campaign-related." Inhofe described it as an attempt by Obama "to shore up his national security credentials, because he has spent the past three years gutting our military."

The U.S. and allies attacked Afghanistan less than a month after the terrorist attacks of Sept. 11, 2001. Coalition forces quickly toppled the ruling Taliban, which had provided safe haven for bin Laden and the al-Qaeda terrorists who plotted 9/11.

In the years since, remnants of the Taliban have continued to battle Karzai's government. Obama -- who said the George W. Bush administration ignored Afghanistan in favor of the war in Iraq -- ordered the deployment of 30,000 more U.S. troops in December 2009.

White House officials scheduled today's trip so that Obama would land in and leave Afghanistan in darkness.

Air Force One departed the Andrews military base near Washington, D.C., shortly after midnight on Tuesday.

The presidential party arrived at Bagram Air Base in Afghanistan at 10:20 p.m. local time.

The president left Afghanistan at 4:25 a.m. local time.

Obama visited amid intense anti-American feelings in some parts of the country.

His visit came within months of riots triggered by the accidental burning of the Quran at a U.S. military base. Also, a U.S. soldier is to stand trial for the shooting deaths of 17 Afghan civilians.

Similar security measures were taken on Obama's two previous trips to Afghanistan, most recently in December of 2010.