A resident of the area in Diyala province said the attack was carried out by the militiamen in revenge for a roadside bomb that struck one of their patrols, an account backed up by MP Nahida al-Daini, who put the death toll at 32.
Interior ministry spokesman Brigadier General Saad Maan supported that version of events, but said the bomb blast had targeted civilians who have volunteered to fight alongside Iraqi security forces, who then randomly opened fire.
The incident at the mosque is all but guaranteed to increase already-high sectarian tensions and widespread anger among Iraq's Sunni Arab minority with the Shiite-led government.
This will undermine government efforts to retake predominantly-Sunni areas overrun by jihadist-led militants, who launched a major offensive in June that swept security forces aside.