Iraq and the United States have finally agreed on a security pact which would mean that US forces would withdraw from Iraq by 2011, American and Iraqi officials said yesterday.

The accord became a major test of strength between the Iraqi government and Washington since negotiations began in March with the Iraqi Prime Minister, Nouri al-Maliki, pictured below, demanding US concessions on the date of the troop withdrawal and immunity for US troops. The pact replaces the UN Security Council resolution enacted after the American invasion of 2003.

The agreement still needs to be approved by the council of Iraqi leaders, the cabinet and the Iraqi parliament. Mr Maliki saw the highly influential Shia religious leader, the Grand Ayatollah Ali al-Sistani, last week and was assured that he would not stand in the way of the pact if approved by parliament.

Download the new Independent Premium app Sharing the full story, not just the headlines

The accord has been on the verge of being signed several times in the past only for fresh objections to be made by the Iraqi government, which has become increasingly confident of its own strength. A compromise has been reached on whether or not US troops can be tried by an Iraqi court if they commit crimes while not engaged in operations. US troops are to withdraw from Iraqi towns and villages by the middle of next year and from Iraq entirely by the middle of 2011 said the government's spokesman, Ali Dabbagh.

He said: "The withdrawal is to be achieved in three years. In 2011, the government at that time will determine whether it needs a new pact or not, and what type of pact will depend on the challenges it faces."

The US administration will present the pact as a sign of its success in Iraq but in fact the accord is very different from originally envisaged by Washington which would largely have continued the occupation as before.

President Bush was opposed to timelines or dates for an American withdrawal and the US is still stressing that this is conditional on improved security in Iraq. But it is unlikely that the Shia majority will want to share power with the US.

Iraqi politicians have always assumed that Washington's insistence on signing a new accord before the presidential election was motivated by the White House's hope that the accord would be seen as a sign that its Iraq policy had at last produced a success. The Republican contender, Senator John McCain, started off his campaign by saying that US troops might stay for 100 years and there should be no date for their withdrawal. The Democratic candidate, Senator Barack Obama, wants combat troops home by the middle of 2010, which was also the date originally proposed by Mr Maliki.

Iraq has faded as an issue in the presidential election as the financial crisis worsened. However, claims that the Republicans had won a victory in Iraq looked increasingly unreal as it became clear that a withdrawal date would be determined by Mr Maliki, and not by the US.

The US has given ground on crucial issues. On the legal immunity of American troops Mr Dabbagh said: "Inside their bases, they will be under American law. Iraqi judicial law will be implemented in case these forces commit a serious and deliberate felony outside their military bases and when off duty." Contractors, who have more men in Iraq than the US army, will no longer have immunity.