Image copyright Reuters Image caption US troops - here seen at the site of a suicide attack in Kabul - are handing over to Afghan security forces across the country

The new Afghan government has signed a security deal with American officials that will allow US troops to remain in the country beyond this year.

The agreement was signed by Afghanistan's newly appointed national security adviser, Hanif Atmar.

Previous President Hamid Karzai had refused to sign the Bilateral Security Agreement (BSA) with the US, straining ties and raising security fears.

Most Nato forces are to withdraw this year, leaving 9,800 US troops behind.

The total number of troops in a new Nato-led mission - including US soldiers - to train and assist Afghanistan's security forces will be about 12,000 next year.

There will also be a separate US-led force dealing with the remnants of al-Qaeda.

US ambassador to Kabul Jim Cunningham signed the long-delayed agreement on behalf of the government in Washington.

Image copyright AFP Image caption The deal comes a day after Mr Ghani (centre) was sworn in as president

Ashraf Ghani, newly elected Afghan President, welcomed the deal. He said: "Today Afghanistan has regained its sovereignty as a power."

Meanwhile US President Barack Obama said the BSA marked a "historic day" in US-Afghan relations.

"We look forward to working with this new government to cement an enduring partnership that strengthens Afghan sovereignty, stability, unity, and prosperity," he added.

The BSA allows for some foreign special forces to stay in the country to conduct "counter-terror operations" and others to support and train Afghan forces.

Under a separate Nato agreement also signed on Tuesday, several nations, led by Germany, Turkey and Italy, will contribute to a further force of about 3,000 troops.

Analysis: David Loyn, BBC News, Kabul

The Bilateral Security Agreement between Afghanistan and the US allows American troops to remain in Afghanistan after the end of 2014. But the force will be quite small, at 9,800 troops, and will be cut in half by the end of 2015, before a full pullout at the end of 2016.

Under a separate Nato agreement, a further force of about 3,000 troops will be contributed by several Nato nations, led by Germany, Turkey and Italy. Britain's only contribution will be at the officers' training academy, which is modelled on Sandhurst, at Qargha close to Kabul.

By agreeing to the deal so quickly, President Ghani is resetting a relationship soured by his predecessor Hamid Karzai, who refused to sign the agreements, and to the end criticised the US and its forces. The US ambassador to Kabul, Jim Cunningham, said that Tuesday's signing sent a broader signal to the region about the continuing US commitment to Afghanistan.

The US deployment will be halved by the end of 2015 and withdrawn almost completely by the end of 2016. The Associated Press news agency reports that the US plans to leave about 1,000 troops in a "security office" after this deadline.

Nato countries have been steadily reducing the number of troops they have committed to the Afghan mission, handing over control to local security forces.

Earlier this year, there were estimated to be just over 50,000 Nato troops serving in Afghanistan from 49 contributing nations. Of these the bulk - about 34,000 - were US troops.

Mr Ghani was sworn in as Afghanistan's new president on Monday, replacing Mr Karzai in the country's first democratic transfer of power.

The Kabul ceremony followed six months of deadlock amid a bitter dispute over electoral fraud and a recount of votes.

Under a US-brokered unity deal, Mr Ghani shares power with presidential poll runner-up Abdullah Abdullah, who becomes chief executive.

Their first joint official act was to oversee the signing of the new BSA deal, and the Nato deal that followed it, in the presidential palace.

Mr Karzai had refused to sign the deal, in part because the US sought immunity from prosecution for its forces.

He also asked for further assurances from the US that its forces would not raid Afghan homes and that Washington would help start stalled peace talks with the Taliban.

Mr Karzai's refusal to sign aggravated relations with the US, and prompted fears that Taliban insurgents would exploit a gap in security.