President Bush ducks as the shoes are thrown
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.
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.
"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.
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.