Media playback is unsupported on your device Media caption President Obama: "In the next two months our troops will pack up their gear and board convoys for the journey home"

All US troops will be pulled out of Iraq by the end of the year, President Barack Obama has announced.

He ordered a complete withdrawal from the country, nearly nine years after the invasion under President George W Bush.

About 39,000 US troops remain in Iraq, down from a peak of 165,000 in 2008.

The US and Iraq were in "full agreement" on how to move forward, Mr Obama said, adding: "The US leaves Iraq with our heads held high."

"That is how America's military efforts in Iraq will end."

While Republicans bicker over the choice of a candidate, President Obama is exciting his own base Mark Mardell, North America editor, BBC Read more from Mark

According to the Department of Defense, there have been 4,408 American military deaths in Iraq since March 2003.

Mr Obama spoke at the White House following a video conference with Iraqi Prime Minister Nouri Maliki.

He pledged assistance and "a strong and enduring partnership" with Iraqi government.

The US declared the end of its combat mission in Iraq in 2010. The deadline for complete troop withdrawal by end of 2011 was set during former President George W Bush's term in office.

However, the issue of a full pullout had been the subject of an ongoing debate.

Iraqi leaders had wanted 5,000 US troops to remain in a training capacity. But those trainers would not have received immunity from prosecution under Iraqi law.

The war in Iraq War began on 19 March 2003 with 173,000 troops, 150,000 of whom were Americans

670,000 Iraqi security forces were on duty as of March 2011

4,408 American troops have been killed

179 British troops have been killed

115,405 Iraqi civilians are estimated to have been killed

32,195 American troops have been wounded Source: Brookings Iraq Index, US Department of Defense

The Pentagon refused to accept that condition, with Defence Secretary Leon Panetta insisting that "we protect and provide the appropriate immunity for our soldiers".

The decision to pull out all US troops suggests no deal could be reached, despite Iraq's desire for continued access to US military expertise, correspondents say.

Earlier this month, Iraqi government spokesman Ali al-Dabbagh said: "If we do not have agreement on the immunity, there will be no agreement on the number."

Many Iraqis are sensitive about the issue, given the number of civilian shootings involving US troops since the US-led invasion. Private contractors have already lost their immunity.

Mr Obama said the withdrawal comes amid changes in American military priorities, including a troop drawdown in Afghanistan, and new political realities in the Middle East and Africa.

"The tide of war is receding," he said.