Kabul, Afghanistan (CNN) -- President Barack Obama, speaking early Wednesday in Afghanistan at the tail end of a surprise visit there, discussed how the war will end and promised a steady drawdown of U.S. troops.
Obama committed to pulling 23,000 troops out of the country by the end of summer and sticking to the 2014 deadline to turn security fully over to the Afghan government. He said that NATO will set a goal this month for Afghan forces to be in the lead for combat operations next year.
"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," the president said during a speech at Bagram Air Base.
His unannounced trip was Obama's third visit to Afghanistan since taking office. It coincided with the first anniversary of the U.S. raid that killed Osama bin Laden in neighboring Pakistan, and comes as Obama is fighting for re-election.
Hours after Obama left the country, the NATO-led International Security Assistance Force reported a loud explosion in the capital, Kabul.
Gen. Mohammad Ayoub Salangi, Kabul chief of police, told CNN it was a suicide car bomb. The blast killed five people outside a compound known as Green Village, according Sediq Seddiqi, a spokesman for Afghan Interior Ministry. An undetermined number of others also were injured.
The president promised not to keep troops in harm's way "a single day longer than is absolutely required for our national security," but promised to "finish the job" and "end this war responsibly."
Obama spoke of a "negotiated peace," and said his administration has been in direct talks with the Taliban. "We've made it clear that they can be a part of this future if they break with al Qaeda, renounce violence, and abide by Afghan laws," he said.
Finally, the president vowed: "This time of war began in Afghanistan, and this is where it will end."
Earlier in his trip, Obama and Afghan President Hamid Karzai signed an agreement outlining cooperation between their countries once the U.S.-led international force withdraws in 2014.
Some U.S. forces will remain in a post-war Afghanistan as military advisers, but both U.S. and Afghan officials have yet to decide how many troops will continue supporting the Afghan military, and for how long.
At a signing ceremony for the Strategic Partnership Agreement, Obama said that neither country asked for the war that began more than a decade ago, but now they would work in partnership for a peaceful future.
"There will be difficult days ahead, but as we move forward in our transition, I'm confident that Afghan forces will grow stronger; the Afghan people will take control of their future," Obama said.
Addressing a concern in Afghanistan that the United States will abandon the country once its troops leave, Obama said, "With this agreement, I am confident that the Afghan people will understand that the United States will stand by them."
He later added that the United States "did not come here to claim resources or to claim territory. We came here with a very clear mission to destroy al Qaeda," referring to the terrorist organization responsible for the September 11, 2001, attacks.
Obama's address came nine years to the day after then-President George W. Bush delivered his "Mission Accomplished" speech aboard the aircraft carrier USS Abraham Lincoln, announcing the end of major combat operations in Iraq.
Karzai offered his thanks to the American people for helping Afghanistan, and the presidents shook hands after signing the document in the atrium of the King's Residence, part of the Presidential Palace in Kabul.
"This agreement will close the season of the past 10 years and is going to open an equal relationship season. With the signing of this agreement, we are starting a phase between two sovereign and independent countries that will be based on mutual respect, mutual commitments and mutual friendship," Karzai said.
Obama warned the Afghan people and, later, U.S. troops he met with, of difficult days ahead. In remarks to troops at Bagram, Obama sounded emotional as he said that soldiers could see friends get hurt or killed as the mission winds down.
"There's going to heartbreak and pain and difficulty ahead, but there's a light on the horizon because of the sacrifices you've made," he said.
The security risks in Afghanistan were evident from the secretive nature and timing of the trip. Obama landed in Afghanistan in the cover of darkness, and the signing ceremony occurred in the late evening.
Back in the United States, politicians reacted to the president's visit -- some with praise, others claiming it was politically motivated.
Presumptive GOP presidential nominee Mitt Romney said, "I am pleased that President Obama has returned to Afghanistan. Our troops and the American people deserve to hear from our president about what is at stake in this war. Success in Afghanistan is vital to our nation's security. It would be a tragedy for Afghanistan and a strategic setback for America if the Taliban returned to power and once again created a sanctuary for terrorists."
Sen. James Inhofe, R-Oklahoma, was less supportive.
"Clearly, this trip is campaign-related. We've seen recently that President Obama has visited college campuses in an attempt to win back the support of that age group since he has lost it over the last three years. Similarly, this trip to Afghanistan is an attempt to shore up his national security credentials, because he has spent the past three years gutting our military," he said in a statement.
The Strategic Partnership Agreement provides a framework for the U.S.-Afghanistan partnership for the decade after the U.S. and allied troop withdrawal, according to senior administration officials who briefed reporters on the flight.
Specific levels of U.S. forces and funding are not set in the agreement and will be determined by the United States in consultation with allies, the officials said on condition of not being identified.
Noting the anniversary of the bin Laden mission, the officials called it a resonant day for the Afghan and American people.
More than 130,000 troops from 50 countries serve in Afghanistan, according to the NATO-led International Security and Assistance Force. The United States is the biggest contributor, providing about 90,000 troops, followed by the United Kingdom (9,500), Germany (4,800) and France (3,600).
The war that began in 2001 is increasingly unpopular in the United States, with the latest CNN/ORC International poll in late March showing 25% of respondents supporting it and 72% opposing it.
More than 2,700 troops from the United States and its partners have died in the war, the majority of them American.
In 2011, the United States outlined its plan to withdraw its troops from Afghanistan by the end of 2014. The move was followed by withdrawal announcements by most of the NATO nations.
Last week, Afghan National Security Adviser Rangin Daftar Spanta and U.S. Ambassador Ryan Crocker initialed a text that outlined the kind of relationship the two countries want in the decade following the NATO withdrawal.
The deal had been long expected after Washington and Kabul found compromises over the thorny issues of "night raids" by U.S. forces on Afghan homes and the transfer of U.S. detainees to Afghan custody.
It seeks to create an enduring partnership that prevents the Taliban from waiting until the U.S. withdrawal to try to regain power, the senior administration officials said.
Obama visited Afghanistan in March 2010 and returned in December of the same year. He also visited Afghanistan in 2008 as a presidential candidate.
CNN's Tom Cohen, Barbara Starr, Keating Holland, Nick Paton Walsh and journalist Masoud Popalzai contributed to this report.