BAGHDAD - Arriving in Baghdad yesterday for a farewell visit, President Bush staunchly defended a war that has taken far more time, money and lives than anticipated, saying the conflict "has not been easy" but was necessary for U.S. security, Iraqi stability and "world peace."

But during a news conference with Iraqi Prime Minister Nouri al-Maliki, Bush received a taste of the immense resentment many Iraqis feel toward his policies: Just after Bush finished his remarks and said "Thank you" in Arabic, an Iraqi journalist took off his shoes and hurled them at Bush, one after the other. The incident lent an air of chaos and farce to a trip intended to highlight improving security conditions.

"This is a farewell kiss!" the man, identified as Muntadar al-Zaidi, a reporter with the Cairo-based network Al Baghdadia Television, yelled as he threw the shoes. Bush had to duck and narrowly missed being hit, according to on-scene reports. Maliki reached out his hand to shield the president.

Zaidi started to yell "Dog, dog!" as he was surrounded by security agents, who tackled him to the floor and began to beat him. Zaidi was later removed.

Bush was not injured and joked about the incident. "All I can report is it is a size 10," he said.

Zaidi was seated in the second row of seats, about 12 feet from Bush's lectern. Colleagues said Zaidi was kidnapped by Shiite militiamen last year and later released. Throwing a shoe at someone is considered the worst possible insult in Iraq.

Early today, Bush made a surprise stop in Afghanistan to review the deteriorating military and political situation in the seven-year-old war. Immediately after arrival, Bush held a rally with U.S. and foreign troops at the massive Bagram Air Base.

Bush began his two-day whirlwind trip through the two war zones in Iraq yesterday under a veil of secrecy. It was his fourth and presumably final stop as president in the war-torn country that will be central to defining his presidency.

Air Force One landed in Baghdad around 4 p.m. local time after a 101/2-hour overnight flight from Andrews Air Force Base. Bush met with U.S. troops and Iraqi leaders about a recently completed security agreement, which calls for the withdrawal of U.S. forces by the end of 2011.

After meeting with Iraqi President Jalal Talabani at Salam Palace, Bush hailed the security agreement as "a reminder of our friendship and as a way forward to help the Iraqi people realize the blessings of a free society."

"The work hasn't been easy, but it has been necessary for American security, Iraqi hope, and world peace," Bush said, adding: "I am just so grateful that I had a chance to come back to Iraq before my presidency ended."

Bush's praise for the security agreement is remarkable given that the U.S. administration spent years dismissing proposals for withdrawal time lines as admissions of defeat. Bush and his aides characterize the deal as a sign of improvement, and Bush said last week that the fight in Iraq was nearing "a successful end."

Yet attacks have continued, and many areas in Iraq remain unstable. Last week, at least 57 Iraqis were killed in a suicide attack at a popular restaurant outside the oil-rich city of Kirkuk.

Bush previously traveled to Iraq in November 2003, June 2006 and September 2007. As with those trips, his fourth was surrounded in secrecy. Bush's office had distributed a bogus public schedule that said he would attend a "Christmas in Washington" event last night at the National Building Museum.

Today's rally in Afghanistan for more than a thousand military personnel took place in the cold predawn hours - it was about 5:30 a.m. local time when Bush strode into the Bagram hangar to loud cheers.

"Afghanistan is a dramatically different country than it was eight years ago," he said. "We are making hopeful gains."

Bush then took a helicopter ride to Afghan President Hamid Karzai's palace in Kabul, the portion of the 40-hour journey that made his security detail the most nervous.

"I and the Afghan people are very proud and honored to the profoundest depth of our hearts to have President Bush with us here today," Karzai said as they sat side by side in the palace.

But his message had a little sting. He emphasized how Bush's visit came only after repeated requests, and he said that he wished that Bush had more time and that the Afghan people could see him in person.

Visit our Web site for more photos and video of the shoe-throwing incident. Also, read Trudy Rubin's blog.