KABUL, Afghanistan (CNN) - President Barack Obama marked the first anniversary of the death of terror mastermind Osama bin Laden with an unannounced trip to Afghanistan, where he reiterated that U.S. troops will not remain in the country "a single day longer" than necessary.
Obama said he remains 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," the president said during a nationally televised speech at Bagram Air Base early Wednesday. "That will be the job of the Afghan people."
Going back to the Vietnam War era, American television networks have covered presidents speaking to military personnel or alongside foreign leaders overseas. But Obama's speech was the first televised address to the nation delivered from a war zone on foreign soil, White House spokesman Josh Earnest said.
Obama's trip was his third to Afghanistan since taking office and comes as he fights for re-election. The president is scheduled to return to Washington around noon Wednesday.
Meanwhile, two coalition service members were killed and two wounded in an explosion targeting their vehicle in Wardak province, Maj. Paul Haverstick said. A U.S. official told CNN the victims were Americans.
And about two hours after Obama left the country, a powerful explosion rocked the capital, Kabul, authorities reported.
A suicide car bomb was detonated outside the gates of Green Village, a compound that houses contractors and aid workers, killing at least seven people and wounding 17 others, the Afghan Interior Ministry said. The casualties included schoolchildren.
"This is another desperate attack by the Taliban," said Gen. Carsten Jacobson, a spokesman for the NATO-led International Security Assistance Force. "Another attack by the insurgency that resulted in the deaths of innocent Afghan civilians, with most of that being children from a nearby school."
The U.N. Assistance Mission in Afghanistan said that the "indiscriminate and illegal attacks are unacceptable and that those responsible are fully accountable for the deaths and injuries of civilians."
In an e-mail, the Taliban denied there were any civilian casualties and said it planned the attack after word circulated that the American president was going to be in Afghanistan. The group also said it will launch its spring offensive Thursday.
In his speech, even as Obama pledged to not keep troops in harm's way "a single day longer than is absolutely required for our national security," he promised to "finish the job" and "end this war responsibly."
Obama also 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.