BAGHDAD  President Bush made a valedictory visit on Sunday to Iraq, the country that will largely define his legacy, but the trip will more likely be remembered for the unscripted moment when an Iraqi journalist hurled his shoes at Mr. Bush’s head and denounced him on live television as a “dog” who had delivered death and sorrow here from nearly six years of war.

The drama unfolded shortly after Mr. Bush appeared at a news conference in Baghdad with Prime Minister Nuri Kamal al-Maliki to highlight the newly adopted security agreement between the United States and Iraq. The agreement includes a commitment to withdraw all American forces by the end of 2011.

The Iraqi journalist, Muntader al-Zaidi, 28, a correspondent for Al Baghdadia, an independent Iraqi television station, stood up about 12 feet from Mr. Bush and shouted in Arabic: “This is a gift from the Iraqis; this is the farewell kiss, you dog!” He then threw a shoe at Mr. Bush, who ducked and narrowly avoided it.

As stunned security agents and guards, officials and journalists watched, Mr. Zaidi then threw his other shoe, shouting in Arabic, “This is from the widows, the orphans and those who were killed in Iraq!” That shoe also narrowly missed Mr. Bush as Prime Minister Maliki stuck a hand in front of the president’s face to help shield him.