KABUL, Afghanistan — President Obama made a surprise trip here on Tuesday to sign a landmark strategic partnership agreement between the United States and Afghanistan in a midnight ceremony meant to mark the beginning of the end of a war that has lasted for more than a decade.


Mr. Obama arrived after nightfall under a veil of secrecy at Bagram Air Base north of Kabul. He then flew by helicopter to the presidential palace, where he and President Hamid Karzai signed the pact, which is intended to be a road map for two nations lashed together by war and groping for a new relationship after the departure of American troops, scheduled for the end of 2014.


“With this agreement the Afghan people, and the world, should know that Afghanistan has a friend and a partner in the United States,” Mr. Obama declared as the Afghan leader looked on. “With this agreement, I am confident that the Afghan people will understand that the United States will stand by them.”


The agreement, Mr. Karzai said, opened “a new chapter in the relationship between the United States and Afghanistan,” one marked by “mutual respect.”


President Obama was scheduled to address the American people from Afghanistan on Tuesday evening, before dawn here. The address, on the first anniversary of the American raid that killed Osama bin Laden in neighboring Pakistan, will give Mr. Obama a new opportunity to make an election-year case that he has wound down two costly and now unpopular wars here and in Iraq.


The agreement, completed after 20 months of arduous negotiations in Washington and Kabul, pledges American support for Afghanistan for 10 years after the withdrawal of the last American soldiers at the end of 2014. More symbol than substance, it nevertheless marks a pivotal transition for the United States from the largest foreign military force in Afghanistan to a staunch, if faraway, ally.


For the president, facing a re-election battle in which his conduct of the war is likely to be debated into November, the visit is laden with political significance. Senior officials said the agreement showcased Mr. Obama’s determination to end the war responsibly, even as they conceded that the country that American troops leave behind will be a messy, violent place, where the Taliban will keep a foothold.


Still, the agreement, a senior administration official told reporters traveling on Air Force One, will give the United States “the capacity to carry out the counterterrorism operations that are necessary for Al Qaeda not to resettle,” and help ensure “a regional equilibrium that serves our national security interest — and that’s ultimately why we went in there in the first place.”


The agreement is also meant to reassure Afghans that the United States will not abandon them once the troops leave — whether to the insurgency of the Taliban or to the meddling of neighbors like Pakistan and Iran. Mr. Obama’s decision to travel to Kabul to sign the document, his aides said, further underlines that commitment.


After he and Mr. Karzai signed the agreement and made brief statements, the president flew back to Bagram, where he greeted troops before delivering a 10-minute address to the nation on Afghanistan, his first in more than a year.


“The reason the Afghans have a new tomorrow is because of you,” Mr. Obama said to several hundred troops assembled in a cavernous hangar, against a backdrop of American flags and several armored vehicles.


The timing of the trip, administration officials said, was dictated by the desire of both presidents to sign the agreement before a NATO summit meeting in Chicago later this month. But it also came just four days before two big campaign rallies that will serve as the symbolic kickoff of Mr. Obama’s re-election bid.


With polls showing a large majority of Americans weary with the war, the president’s aides have discussed whether to accelerate current plans, which call for withdrawing 22,000 troops by September. In Chicago, the United States and NATO allies will ratify a shift in the mission in 2013 from a combat role to one focused on counterterrorism and training of Afghan security forces, though officials said the White House would not announce any additional troop reductions there.