Even the fanatics of the Islamic State of Iraq and the Levant (Isis) are astonished at the extent of their own victory in taking control over Iraq’s second city, Mosul, in the past week. “Enemies and supporters alike are flabbergasted,” said Isis spokesman Abu Mohammed al-Adnani. It is difficult to think of any examples in history when security forces almost a million strong, including 14 army divisions, have crumbled so immediately after attacks from an enemy force that has been estimated at between 3,000 and 5,000 strong.

It is a rout without precedent. I have written frequently in the past in this newspaper that the Iraqi security forces were a corrupt patronage machine that exploited and persecuted the local population. It was significant that for the first six months of this year, Isis secured its grip on Fallujah, 40 miles west of Baghdad, without any sustained effort by the army to dislodge them other than indiscriminate bombing and shelling of the city.

In March Isis even held a parade in Abu Ghraib, where the infamous prison had to be hastily evacuated, and which is a dozen miles from the centre of the Iraqi capital. A friend in Baghdad was shocked and half-amused to learn of Isis’s presence from a pro-government television news channel that announced “a great triumph by the Iraqi security forces in defeating the terrorists east and west of Abu Ghraib”.

Corruption in the army took place at every level. A general could become a divisional commander at a cost of $2m (£1.18m) and would then have to recoup his investment from kickbacks at checkpoints on the roads, charging every goods vehicle. An Iraqi businessman told me some years ago that he had stopped importing goods through Basra port as unprofitable because of the amount of money he had to spend bribing officials and soldiers at every stage as his goods were moved from the ship at the dockside to Baghdad.

Unsurprisingly, Iraqi soldiers and police were not prepared to fight and die in their posts resisting Isis last week, since their jobs were always primarily about making money for their families.

Another friend in Baghdad (I am afraid any account of Iraq will always be littered with sources who wish to remain anonymous) told me: “Soldiers under Saddam Hussein often wanted to desert – they were scarcely paid – but they knew they would be killed if they did, so it was better to die in battle. The present army has never been a national army, its soldiers were only interested in their salaries and they were no longer frightened of what would happen to them if they ran away.”

Military units never took part in training exercises and most soldiers only knew how to use a Kalashnikov assault rifle. There are a few trained and well-equipped Swat teams of anti-terrorist forces that are effective but not numerous.

In Sunni areas the army and security forces behaved as an occupation force and were consequently much feared and hated. Frightening and bloodthirsty Isis fighters may be, but for many in Mosul they are preferable to government forces. Sunni men were alienated by not having a job because government funds were spent elsewhere and, on occasion, suddenly sacked without a pension for obligatory membership of the Ba’ath party decades earlier. One Sunni teacher with 30 years’ experience one day got a crumpled note under his door telling him not to come to work at his school any more because he had been fired for this reason. “What am I to do? How am I going to feed my family?” he asked.

Sectarian discrimination and persecution became the common lot of Iraq’s five or six million Sunni who had been the dominant community for centuries. A Sunni might be picked up by the police, tortured into a confession, sentenced to a long term in prison or even executed. Even if he was found innocent by a court, his family might have to pay $50,000 to $100,000 to get an officer in the prison to sign his release papers. An Isis fighter was recently reported as joking: “When we capture our enemies we kill them; when you capture one of us we pay money and he is released.”

Anger at these abuses is relevant to what is now happening. The majority of Sunni Arabs in Mosul – attitudes will be different among Kurds and minorities – are wary of Isis but terrified of what a vengeful Iraqi army will do if it retakes the city. Past experience, based on what happened in Mosul in 2003 when insurgents briefly took the city, shows that Sunni men, regardless of their actions or sympathies, will be vulnerable to arrest, torture and execution. Isis may have seized Mosul with a small force, but if the Iraqi army tries to take it back tens of thousands of Sunni will fight to defend it.

The same is true in the rest of Sunni Iraq. Isis may have begun the assault, but many other groups have joined in. We are now looking at a general uprising of the Iraqi Sunni. Those taking over Saddam Hussain’s hometown of Tikrit are not Isis, but his old adherents who are putting up posters of the late dictator.

Mosul is a traditional recruiting ground for the officer corps of the old Iraqi army. Much will depend how far Isis is capable of moderating its policies in order to accommodate more secular or Ba’athist opponents of the Baghdad government. In 2006-7 it alienated other Sunni by its brutality, enabling the US to win them over and isolate al-Qa’ida in Iraq. The present Iraqi government has got the worst of all possible worlds by persecuting the Sunni enough to enrage and unite them, but without crushing them.

ORDER IT NOW

The incompetence of the government in Baghdad explains many but not all the disasters of the last week. Isis were the shock troops of a much broader group of Sunni militant groups such as al-Naqshbandi army and assorted Ba’athist groups. Attacks were well coordinated and planned and were probably assisted by Sunni army officers within the regular Iraqi army sabotaging the defence.

Much attention is given in the media to what the US will now do. But Americans are not the players they once were in Iraq. More important is how Iran reacts. After the overthrow of Saddam, it struggled with the US for influence in Iraq for six or seven years and eventually emerged as the main foreign influence in the country.

There was also a degree of mostly covert cooperation between the US and Iran in opposition to Saddam pre-2003 and afterwards to install a stable Shia-Kurdish government in the face of an insurgency. Their differences stemmed from rivalry about who should be the dominant power in post-Saddam Iraq.

Iraq matters more to Iran than Syria. It is also better placed than the US to help the beleaguered Iraqi government. The Iraqi army and its commanders are wholly discredited. The Iranian Revolutionary Guard Corps is now moving into Baghdad to reorganise a new military force that would combine elements of the old military and the militias, some of which are already under Iranian control. The aim of this would be to hold Baghdad and probably a line to the north through mixed Sunni-Shia provinces such as Diyala and cities including Samarra with its Shia shrine, destruction of which in 2006 led to the most savage stage of the Sunni-Shia civil war.

US and Britain must work with Iran if they are to stop an extreme Sunni state emerging in north and west Iraq extending into eastern Syria.