Britain will wrap up its military mission in Iraq by June 1, British Prime Minister Gordon Brown said, in Baghdad on Wednesday for an unannounced visit.

Brown was received by Iraqi Prime Minister Nuri al-Maliki for his fourth visit to the country. Iraq's cabinet drafted a law on Tuesday that will facilitate Britain's withdrawal from the country.

The legislation calls for all foreign soldiers in Iraq, with the exception of the United States, to leave the country by July 1.

"We thank [British troops] for the efforts they have made for getting rid of dictatorship and terrorism. They have made a lot of sacrifices," al-Maliki said at a press conference.

Britain had previously said the mission would conclude in July. But Brown told reporters Wednesday that Britain has reached an agreement with Iraq to conclude its mission by May 31 and then withdraw its troops within two months.

"The role played by the U.K. combat forces is drawing to a close. These forces will have completed their tasks in the first half of 2009 and will then leave Iraq," Brown and al-Maliki said in a joint statement.

Britain is expected to begin withdrawing its 4,100 troops in March following January's regional elections.

About 400 troops are expected to be left in southern Iraq after the withdrawal and a United States brigade will replace the British force at the Basra airport.

Britain had about 40,000 troops in Iraq at the height of the U.S.-led invasion in 2003.

With files from the Associated Press