As he leaves Iraq this week, the outgoing US commander, General David Petraeus, is sounding far less optimistic than the Republican presidential candidate, John McCain, about the American situation in Iraq. General Petraeus says that it remains "fragile", recent security gains are "not irreversible" and "this is not the sort of struggle where you take a hill, plant the flag and go home to a victory parade... it's not a war with a simple slogan."

Compare this with Sarah Palin's belief that "victory in Iraq is wholly in sight" and her criticism of Barack Obama for not using the word "victory". The Republican contenders have made these claims of success for the "surge" – the American reinforcements sent last year – although they are demonstrably contradicted by the fact that the US has to keep more troops, some 138,000, in Iraq today than beforehand. Another barometer of the true state of security in Iraq is the inability of the 4.7 million refugees, one in six of the population, who fled for their lives inside and outside Iraq, to return to their homes.

Ongoing violence is down, but Iraq is still the most dangerous country in the world. On Friday a car bomb exploded in the Shia market town of Dujail, north of Baghdad, killing 32 people and wounding 43 others. "The smoke filled my house and the shrapnel broke some of the windows," said Hussein al-Dujaili. "I went outside the house and saw two dead bodies at the gate which had been thrown there by the explosion. Some people were in panic and others were crying."

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

Playing down such killings, the Iraqi government and the US have launched a largely successful propaganda campaign to convince the world that "things are better" in Iraq and that life is returning to normal. One Iraqi journalist recorded his fury at watching newspapers around the world pick up a story that the world's largest Ferris wheel was to be built in Baghdad, a city where there is usually only two hours of electricity a day.

Life in Baghdad certainly is better than it was 18 months ago, when some 60 to 100 bodies were being found beside the roads every morning, the victims of Sunni-Shia sectarian slaughter. The main reason this ended was that the battle for Baghdad in 2006-07 was won by the Shia, who now control three-quarters of the capital. These demographic changes appear permanent; Sunni who try to get their houses back face assassination.

In Mosul, Iraq's northern capital and third largest city, with a population of 1.8 million, the government was trumpeting its success only a few months ago. It said it had succeeded in driving al-Qa'ida from the city, while the US said the number of attacks had fallen from 130 a week to 30 a week in July. But today they are back up to between 60 and 70 a week. Two weeks ago, insurgents came close to killing Major-General Riyadh Jalal Tawfiq in Nineveh province, of which Mosul is the capital, with a roadside bomb.

The perception in the US that the tide has turned in Iraq is in part because of a change in the attitude of the foreign, largely American, media. The war in Iraq has now been going on for five years, longer than the First World War, and the world is bored with it. US television networks maintain expensive bureaux in Baghdad, but little of what they produce gets on the air. When it does, viewers turn off. US newspaper bureaux are being cut in size. The result of all this is that the American voter hears less of violence in Iraq and can suppose that America's military adventure there is finally coming good.

An important reason for this optimism is the fall in the number of American soldiers killed. (The 30,000 US soldiers wounded in Iraq are seldom mentioned.) This has happened because the war that was being waged against the American occupation by the Sunni community, the 20 per cent of Iraqis who were in control under Saddam Hussein, has largely ended. It did so because the Sunni were being defeated, not so much by the US army as by the Shia government and the Shia militias.

Sunni insurgent leaders who were nationalists or Baathists realised that they had too many enemies. Not only was al-Qa'ida trying to take over from traditional tribal leaders, it was also killing Sunni who took minor jobs with the government. The Awakening, or al-Sahwa, movement of Sunni fighters was first formed in Anbar province at the end of 2006, but it was allied to the US, not the Iraqi government. This is why, despite pressure from General Petraeus, the government is so determined not to give the 99,000 al-Sahwa members significant jobs in the security forces when it takes control of – and supposedly begins to pay – these Sunni militiamen from 1 October. The Shia government may be prepared to accommodate the Sunni, but not at the cost of diluting Shia dominance.

If McCain wins the presidential election in November, his lack of understanding of what is happening in Iraq could ignite a fresh conflict. In so far as the surge has achieved military success, it is because it implicitly recognises America's political defeat in Iraq. Whatever the reason for President George Bush's decision to invade Iraq and overthrow Saddam Hussein in 2003, it was not to place the Shia Islamic parties in power and increase the influence of Iran in the country; yet that is exactly what has happened.

The surge only achieved the degree of success it did because Iran, which played a central role in getting Nouri al-Maliki appointed Prime Minister in 2006, decided to back his government fully. It negotiated a ceasefire between the Iraqi government and the powerful movement of Muqtada al-Sadr in Basra, persuading the cleric to call his militiamen off the streets there, in March and again two months later in the Sadrist stronghold of Sadr City. It is very noticeable that in recent weeks the US has largely ceased its criticism of Iran. This is partly because of American preoccupation with Russia since the fighting began in Georgia in August, but it is also an implicit recognition that US security in Iraq is highly dependant on Iranian actions.

General Petraeus has had a measure of success in Iraq less because of his military skills than because he was one of the few American leaders to have some understanding of Iraqi politics. In January 2004, when he was commander of the 101st Airborne Division in Mosul, I asked him what was the most important piece of advice he could give to his successor. He said it was "not to align too closely with one ethnic group, political party, tribe, religious group or social element". But today the US has no alternative but to support Mr Maliki and his Shia government, and to wink at the role of Iran in Iraq. If McCain supposes the US has won a military victory, and as president acts as if this were true, then he is laying the groundwork for a new war.