KABUL/WASHINGTON - The United States and NATO formally ended their war in Afghanistan on Sunday with a ceremony at their military headquarters in Kabul as the insurgency they fought for 13 years remains as ferocious and deadly as at any time since the 2001 invasion that unseated the Taliban regime following the Sept 11 attacks.

The low-key ceremony was arranged in secret due to the threat of Taliban strikes in the Afghan capital, which has been hit by repeated suicide bombings and gun attacks over recent years.

“Together... we have lifted the Afghan people out of the darkness of despair and given them hope for the future,” NATO commander US General John Campbell told assembled soldiers. “You’ve made Afghanistan stronger and our countries safer.”

On January 1, the US-led International Security Assistance Force (ISAF) combat mission, which has suffered 3,485 military deaths since 2001, will be replaced by a NATO “training and support” mission.

About 12,500 foreign troops staying in Afghanistan will not be involved in direct fighting, but will assist the Afghan army and police in their battle against the Taliban, who ruled from 1996 until 2001.

When numbers peaked in 2011, about 130,000 troops from 50 nations were part of the NATO military alliance.

“I hope you take great pride in the positive impact you’ve made and will continue to make upon the Afghan people,” Campbell said in a speech released by ISAF on Twitter as live broadcasts were banned for security reasons.

Campbell folded up the ISAF flag and unveiled the flag of the new mission, named Resolute Support.

Sunday’s ceremony - held in a sports hall at NATO headquarters - completed the gradual handover of responsibility to the 350,000-strong Afghan forces, who have been in charge of nationwide security since last year.

But recent bloodshed has undermined claims that the insurgency is weakening and has highlighted fears that the international intervention has failed as Afghanistan faces spiralling violence.

The United Nations says that civilian casualties hit a record high in 2014, jumping by 19 percent with 3,188 civilians killed by the end of November. Afghan’s police and army have also suffered a grim death toll, with fatalities soaring to more than 4,600 in the first 10 months of 2014 - far higher than all ISAF deaths since 2001.

“The US and NATO mission was an absolute failure as today’s ceremony shows,” Taliban spokesman Zabihullah Mujahid told AFP.

“They are fleeing from Afghanistan. They have not reached their goals in defeating the Afghan Mujahideen.”

US and Afghan commanders insist the national security forces can hold the line against the Taliban despite concerns of a repeat of Iraq, where an American-trained army virtually collapsed in the face of a jihadist onslaught.

“Our sons and daughters of ANSF (Afghan National Security Forces) are in the lead, fighting to protect security interests. God willing, they will prevail,” national security adviser Mohmmad Hanif Atmar said at the ceremony.

Since 2001, billions of dollars of aid have been spent in Afghanistan on new schools, hospitals, roads and promoting women’s rights, but corruption has been endemic and progress limited even in the cities.

In Washington, President Barack Obama saluted Sunday the “milestone” end of NATO’s combat mission in Afghanistan, but warned the country remains “a dangerous place.”

Obama called the ceremony ‘a milestone for our country’. “For more than 13 years, ever since nearly 3,000 innocent lives were taken from us on 9/11, our nation has been at war in Afghanistan,” he said in a statement.

“Now, thanks to the extraordinary sacrifices of our men and women in uniform, our combat mission in Afghanistan is ending, and the longest war in American history is coming to a responsible conclusion.”

Obama thanked the troops and intelligence workers who served in Afghanistan, crediting them with “devastating the core Al-Qaeda leadership, delivering justice to Osama bin Laden, disrupting terrorist plots and saving countless American lives.”

“We are safer, and our nation is more secure, because of their service.” And he honoured those who were wounded or killed in the long war. But, Obama warned, “Afghanistan remains a dangerous place, and the Afghan people and their security forces continue to make tremendous sacrifices in defense of their country.”