Image copyright AFP Image caption Mr Karzai's relations with the US have been troubled for years

President Hamid Karzai has used his last speech in office to warn the new Afghan government to beware of the US.

In a national address before leaving the presidential palace, Mr Karzai said the peace process had failed because "America did not want peace".

The US ambassador said the remarks dishonoured America's war dead. Mr Karzai has long attacked the US for causing too many civilian casualties.

A national unity government takes power on Monday after a bitterly fought vote.

In his farewell speech, Mr Karzai said the war was not among Afghans but "for the objectives of foreigners".

He said a friendly relationship with the United States was possible, but only if their words matched their actions. He strongly criticised Afghanistan's neighbour Pakistan, too, saying it wanted to control Afghan foreign policy.

"If America and Pakistan really want it, peace will come to Afghanistan," Mr Karzai said.

"War in Afghanistan is based on the aims of foreigners. The war in Afghanistan is to the benefit of foreigners. But Afghans on both sides are the sacrificial lambs and victims of this war."

US ambassador to Afghanistan James Cunningham said he was saddened by what Mr Karzai had said.

"His remarks, which were uncalled for, do a disservice to the American people and dishonour the huge sacrifices Americans have made here... that's the part that is ungracious and ungrateful.

"However I am absolutely confident in reassuring Americans that Afghan themselves absolutely value and are grateful for the sacrifices and commitment of the United States to the future of this country."

The US and other donors have spent billions of dollars in Afghanistan since ousting the Taliban in 2001. Several thousand foreign soldiers have lost their lives in Afghanistan and many more have been wounded.

The BBC's David Loyn in Kabul says the very public argument follows months of negotiations - led by the US and the UN after the disputed election - to create the national unity government.

The US will be confident of a better reception, in public and private, from new President Ashraf Ghani, our correspondent says.

When the new administration takes over next week, one of its first acts will be to sign the security deal with the US that was rejected by President Karzai.

The outgoing president was presented with a traditional turban and robe that he immediately passed on to his two vice-presidents, then he left the room.

He was later due to leave the presidential palace for his new home nearby.

Under the unity deal, Mr Ghani becomes president while runner-up Abdullah Abdullah will nominate someone to a post similar to that of prime minister.

The audited results of the presidential election have not been released.

Image copyright Reuters Image caption Abdullah Abdullah (l) and Ashraf Ghani signed the power-sharing agreement in the Presidential Palace in Kabul on Sunday