A few hundred Islamic State insurgents swept into Mosul three years ago as the Iraqi army collapsed, imposing a reign of terror and declaring a caliphate in territory it had seized spanning swathes of Iraq and Syria.