A surprise visit by US President George Bush to Iraq has been overshadowed by an incident in which two shoes were thrown at him during a news conference.
An Iraqi journalist was wrestled to the floor by security guards after he called Mr Bush "a dog" and threw his footwear, just missing the president.
The US president has now continued to Afghanistan to inspect troops there.
He arrived before dawn at Bagram air force base, and is due to hold talks with President Hamid Karzai.
Earlier in Baghdad, Mr Bush and Iraqi PM Nouri Maliki signed the new security agreement between their countries.
The pact calls for US troops to leave Iraq in 2011 - eight years after the 2003 invasion that has in part defined the Bush presidency.
If you want the facts, it's a size 10 shoe that he threw
US President George W Bush
Bush shoe-ing worst Arab insult
Speaking just over five weeks before he hands over power to Barack Obama, Mr Bush also said the war in Iraq was not over and more work remained to be done.
His previously unannounced visit came a day after Defence Secretary Robert Gates told US troops the Iraq mission was in its "endgame".
In the middle of the news conference with Mr Maliki, Iraqi television journalist Muntadar al-Zaidi stood up and shouted "this is a goodbye kiss from the Iraqi people, dog," before hurling a shoe at Mr Bush which narrowly missed him.
Showing the soles of shoes to someone is a sign of contempt in Arab culture.
Muntadar al-Zaidi was quickly wrestled to the ground and hauled away
With his second shoe, which the president also managed to dodge, Mr Zaidi said: "This is for the widows and orphans and all those killed in Iraq."
Mr Zaidi, a correspondent for Cairo-based al-Baghdadiya TV, was then wrestled to the ground by security personnel and hauled away.
"If you want the facts, it's a size 10 shoe that he threw," Mr Bush joked afterwards.
Al-Baghdadiya's bureau chief told the Associated Press that he had no idea what prompted Mr Zaidi to attack President Bush, although reports say he was once kidnapped by a militia and beaten up.
"I am trying to reach Muntadar since the incident, but in vain," said Fityan Mohammed. "His phone is switched off."
Correspondents said the attack was symbolic. Iraqis threw shoes and used them to beat Saddam Hussein's statue after his overthrow.
Mr Bush's first stop upon arriving in Baghdad was the Iraqi presidential palace in the heavily-fortified Green Zone, where he held talks with President Jalal Talabani.
PREVIOUS BUSH VISITS TO IRAQ
Nov 2003: Serves Thanksgiving dinner to troops in Baghdad
June 2006: Meets new Iraqi Prime Minister Nouri Maliki
Sept 2007: Visits Anbar province - former stronghold of Saddam Hussein
"The work hasn't been easy but it's been necessary for American security, Iraqi hope and world peace," Mr Bush said during his talks with Mr Talabani.
The Iraqi president called Mr Bush "a great friend for the Iraqi people, who helped us liberate our country".
The BBC's Humphrey Hawksley, in Baghdad, says the key issue at present is exactly how American troops will withdraw within the next three years and what sort of Iraq they will leave behind.
President Bush said events have been necessary for US security and world peace
The US media has just published details of a US government report saying that post invasion reconstruction of Iraq was crippled by bureaucratic turf wars and an ignorance of the basic elements of Iraqi society.
The report is circulating among US officials in draft form, says the New York Times.
It reveals details of a reconstruction effort that cost more than $100bn (£67bn) and only succeeded in restoring what was destroyed in the invasion and the widespread looting that followed it, the newspaper said.
George Bush says being pelted with shoes could be one of the 'weirdest' moments of his presidencyMr Bush's visit, unannounced in advance and conducted under tight security, follows the approval last month of a security pact between Washington and Baghdad that calls for US troops to be withdrawn from Iraq by the end of 2011.
US troops are first to withdraw from Iraqi cities, including Baghdad, by June next year.
Defence Secretary Gates said on Saturday that "the process of the drawdown" had begun.
"We are, I believe, in terms of the American commitment, in the endgame here in Iraq," he told US troops at an airbase near Baghdad.
Mr Gates has been picked to stay on as defence secretary by President-elect Barack Obama.
The end in sight for US troops in Iraq?
President Bush leaves the White House in less than six weeks. He said in a recent interview with ABC News that the biggest regret of his presidency was the false intelligence that Iraq had weapons of mass destruction.
Finding these was one of the key justifications for the invasion. None were ever found.
Mr Obama has promised to bring home US combat troops from Iraq in a little over a year from when he takes office in January.
More than 4,200 US troops and tens of thousands of Iraqi civilians and security personnel have been killed since the invasion in 2003.
There are currently about 149,000 US soldiers in Iraq, down from last year's peak of 170,000 after extra troops were poured in to deal with a worsening security situation.
As Mr Bush arrived in Baghdad, Gen David Petraeus, the head of the US Central Command, which includes Iraq, said attacks in the country had dropped from 180 a day in June 2007 to 10 a day now.
In a sign of modest security gains in Iraq, Mr Bush was welcomed with a formal arrival ceremony - a flourish that was not part of his previous three visits.
He arrived in the country on Air Force One, which landed at Baghdad International Airport in the afternoon, after a secretive Saturday night departure from Washington on an 11-hour flight.