With the UK drawing to the close of its 13-year military involvement in the country, the Prime Minister said that Britain had gone "a long way" towards achieving its objectives there. Combat troops are set to leave the country by the end of the year.
Cameron was meeting newly installed president Ashraf Ghani - who took office just four days ago - and chief executive Abdullah Abdullah.
The two presidential rivals finally came to a power-sharing agreement after a dispute over the outcome of the elections threatened to plunge the country into renewed turmoil.
"Britain has paid a heavy price for helping to bring stability to this country," the PM said.
"But this is where al Qaida trained their terrorists. This is where 9/11 and countless terror plots were hatched.
"An Afghanistan free from al Qaida is in our national interest - as well as Afghanistan's. And now, 13 long years later, Afghanistan can - and must - deliver its own security.
"The work of defeating Islamist extremist terror goes on elsewhere in the world. And because this threatens us at home, we must continue to play our part."
The Prime Minister paid tribute to the 453 UK servicemen and women who have died in the course of operations in the country as well as to those who had been injured.
"[The armed forces] have paid a very high price for our engagement in Afghanistan. They have done vital work here," he said.
"We should remember those who paid the ultimate price and those who were injured through the work they did."
"The promise I made in 2010 that Britain will be out of the country by 2014, that commitment has been met. That does not mean we are leaving this country to its devices. We have a huge aid programme that will continue and we will pay our share to the Afghan security forces."
Cameron, making his thirteenth visit to Afghanistan, said the country had been transformed since international military operations began in the wake of the 9/11 attacks on the Twin Towers in New York.
In that time they have sought deny al Qaida a safe haven while creating an Afghan army and police force capable of delivering security in their own country.
"I think we have gone a long way to achieving that," he said. "I think that is a very real achievement."
Cameron stressed that ultimately Britain's involvement had been driven by the need to protect the UK from international terrorism.
"The core UK mission was about our own domestic security. That required an Afghan army and police capable of taking care of their own security and denying al Qaida a safe haven," he said.
But while al Qaida had been driven out of Afghanistan, he said that the fight against Islamic extremism continued.
"We are fighting a generational struggle against Islamist extremist terrorists. This is a battle we are going to be engaged in for many, many years," the Prime Minister said.
Cameron said the Afghan forces were now "capable and determined", in a joint press conference with Afghan president Ghani.
"This year again, Afghan forces have proved to the Taliban once again that their aims will not be achieved through violence and intimidation," Cameron said.
"If the Taliban want to secure a role in the future of Afghanistan then they must accept that they have to give up violence and engage in the political process."
Cameron said the UK would continue to support the Afghan National Officer academy in the capital, and would be providing £178 million a year until 2017 to "sustain the major progress" the country had made on education, health and other public services.
Cameron and Ghani will be co-hosting a London conference on Afghanistan in November.
Cameron added: "Together we have made Afghanistan safer, we have made Britain safer.
"Of course the new Government of National Unity faces many challenges ahead but President Ghani and chief executive Abdullah have already shown the leadership that will be necessary to build a better Afghanistan."Suggest a correction