Tony Blair made an emotional tribute to British troops during his seventh and probably final visit to Iraq as PM.
Tony Blair said troops did vital work despite constant threats
He told servicemen and women at the British HQ in Basra, southern Iraq, that they were doing "brilliant" work.
Minutes later, two mortars exploded, rocking the building - which typically receives two such attacks a day.
Earlier, Mr Blair also brushed aside mortar attacks on Baghdad's Green Zone just as he arrived. He hailed "signs of change and progress" in Iraq.
Both the Basra headquarters and Green Zone occupy large areas and come under frequent attack.
No-one is believed to have been hurt in the Basra attack on Saturday, and officials stressed that Mr Blair was not in any danger - but it is thought at least one person was injured in the Baghdad explosions.
BBC defence correspondent Paul Wood, who has been travelling with the prime minister, said the Basra headquarters received "two or three big impacts", which caused its windows to rattle.
"It was not a near miss but still it is a very telling example for the prime minister of the danger facing the troops and ordinary Iraqis," he said.
Mr Blair began his unannounced visit to Iraq in Baghdad on Saturday morning, later moving on to Basra.
In Basra, Mr Blair told troops: "This will be my last chance to say thank you for the work that you have done here."
He said they had carried on their vital work despite constant threats and mortar and rocket attacks.
Mr Blair met Iraqi PM Nouri Maliki in Baghdad
"You have done it and you have done it absolutely brilliantly."
Mr Blair said some people questioned why troops were in Iraq, but he said what they were doing was "of importance to the future of not just Iraq but the rest of the world".
Mr Blair also chatted to groups of servicemen and women over cups of tea, to hear about the problems and challenges they faced.
Referring to kick-off time at Wembley, he joked: "I'm under strict instructions to wind everything up before the Cup final begins."
After receiving further detailed military briefings from UK commanders at the HQ, Mr Blair was due to fly back to London.
Speaking in Baghdad before moving on to Basra, Mr Blair insisted that things were improving in Iraq - in spite of the mortar attacks.
After talks with Iraqi Prime Minister Nouri Maliki and President Jalal Talabani, he told a news conference: "There are mortar attacks and terrorist attacks happening every day, that's the reality.
"The question is, what are we going to do in the face of these attacks?
"The answer is, we don't give in to them. The very purpose of the attacks, the suicide bombs, the mortars aimed in here, is so that you will carry nothing but that on your news and won't actually talk about the progress that's happening here."
He added: "Plainly the security situation remains very difficult but on the other hand there are real signs of change and progress also."
Mr Blair appeared irritated at repeated news conference questions about levels of violence.
He asked one reporter: "Why don't you listen to what the person who is the president of Iraq says about Iraq. They are not saying there's no violence or terrorism in Iraq but they are saying there's also change."
Mr Blair insisted he had "no regrets whatsoever" about removing Saddam.
And he warned Iran "has got to understand it can't support terrorism".
Earlier on Saturday, former US President Jimmy Carter criticised the UK prime minister for his "blind" support of the war in Iraq.
Mr Carter told the BBC the UK's "almost undeviating" support for "the ill-advised policies of President George W Bush in Iraq had been a major tragedy for the world".