A file photo dated 22 June 2011 shows a US soldier (C) taking out the American flag as US Army personnel is preparing to leave Mendin military base in the Iraq-Iran borders, southern Iraq. (Haider Al-Assadee / EPA)

President Barack Obama’s announcement on Friday that all 40,000 U.S. troops still in Iraq will leave the country by New Year’s Eve will, inevitably, draw howls of derision from GOP presidential hopefuls — this is, after all, early election season. But the decision to leave Iraq by that date was not actually taken by President Obama — it was taken by President George W. Bush, and by the Iraqi government.

In one of his final acts in office, President Bush in December of 2008 had signed a Status of Forces Agreement (SOFA) with the Iraqi government that set the clock ticking on ending the war he’d launched in March of 2003. The SOFA provided a legal basis for the presence of U.S. forces in Iraq after the United Nations Security Council mandate for the occupation mission expired at the end of 2008. But it required that all U.S. forces be gone from Iraq by January 1, 2012, unless the Iraqi government was willing to negotiate a new agreement that would extend their mandate. And as Middle East historian Juan Cole has noted, “Bush had to sign what the [Iraqi] parliament gave him or face the prospect that U.S. troops would have to leave by 31 December, 2008, something that would have been interpreted as a defeat… Bush and his generals clearly expected, however, that over time Washington would be able to wriggle out of the treaty and would find a way to keep a division or so in Iraq past that deadline.”

But ending the U.S. troop presence in Iraq was an overwhelmingly popular demand among Iraqis, and Prime Minister Nuri al-Maliki appears to have been unwilling to take the political risk of extending it. While he was inclined to see a small number of American soldiers stay behind to continue mentoring Iraqi forces, the likes of Shi’ite cleric Moqtada al-Sadr, on whose support Maliki’s ruling coalition depends, were having none of it. Even the Obama Administration’s plan to keep some 3,000 trainers behind failed because the Iraqis were unwilling to grant them the legal immunity from local prosecution that is common to SOF agreements in most countries where U.S. forces are based.

So, while U.S. commanders would have liked to have kept a division or more behind in Iraq to face any contingencies — and, increasingly, Administration figures had begun citing the challenge of Iran, next door — it was Iraqi democracy that put the kibosh on that goal. The Bush Administration had agreed in 2004 to restore Iraqi sovereignty, and in 2005 put the country’s elected government in charge of shaping its destiny. But President Bush hadn’t anticipated that Iraqi democracy would see pro-U.S. parties sidelined and would, instead, consistently return governments closer to Tehran than they are to Washington. Contra expectations, a democratic Iraq has turned out to be at odds with much of U.S. regional strategy — first and foremost its campaign to isolate Iran.

The Iraq that U.S. forces will leave behind is far from stable, and the mounting tension between Iran and Saudi Arabia could well see a renewed flare-up of Iraq’s disastrous sectarian civil war. A jihadist Sunni insurgency has reasserted itself in recent months with a steady uptick in terror attacks, and it could become a vehicle for Saudi proxy warfare against Iran, which backs the Maliki government and various Shi’ite political and military formations, including Sadr’s. Kurdish-Arab tensions are growing in the north, where the fate of such contested cities as Kirkuk remains unresolved and a source of mounting security danger. Iraq’s political future, also, remains contested, with sectarian and ethnic rivalries reflected in the continued failure to pass a low regulating the sharing of oil revenues, and mounting anxiety over the increasingly authoritarian approach of Prime Minister Maliki.

Iraq could yet fail as a state. But it’s not as if the presence of 40,000 U.S. troops has been all that’s holding it together: Those forces no longer patrol Iraq’s cities, and are mostly involved in mentoring Iraqi units, although they have played a major role in mediating Arab-Kurdish conflicts in the north.

Given the unresolved political conflicts that continue to plague the country even after its transition to democratic government — and in light of the rising levels of regional tension — chances are high that the U.S. withdrawal will be preceded and followed by a sharp uptick in violence. Shi’ite insurgent groups are likely to escalate attacks on U.S. forces, hoping to claim credit for driving out the Americans — and, no doubt, to please their Iranian backers. Sunni insurgent groups are likely to raise their own game, in order to challenge the Shi’ite dominated government and demonstrate its inability to ensure security — an exercise that will suit the agenda of their own backers.

The key to ensuring security after a U.S. withdrawal has always been achieving a regional consensus on Iraq that could set the terms for political compromise inside Iraq — or, at least, limit the likelihood of renewed violence. Unfortunately, instead, that withdrawal coincides with a sharp escalation in the Saudi-Iranian cold war, and that will spell trouble for Iraq.

Not that the U.S. will be out of the picture, by any stretch of the imagination. As things stand, the U.S. embassy in Iraq will have 17,000 employees — including at least 5,000 “security contractors”, i.e. non-uniformed military personnel. It’s not hard to imagine that future training needs of the Iraqi military will be undertaken by privateers rather than under the auspices of the Pentagon. And that the CIA — now under the command of Gen. David Petraeus, former U.S. commander in Iraq — will play a more active role in pursuing U.S. objectives on the ground and in the neighborhood.

But as of December 31, no more American soldiers will be doing tours of duty in Iraq. The war that ousted Saddam Hussein, unleashing an insurgency that left 4,500 Americans and hundreds of thousands of Iraqis dead, and which will cost the U.S. upwards of $1 trillion, is finally over. Historians will note that the U.S. invasion of Iraq precipitated dramatic changes across the Middle East political landscape in the ensuing decade. But many of those changes were hardly the ones the war’s authors had in mind.