* Fighting shuts main oil refinery
* Government forces say they have repelled attack on Baquba
* Dead include prisoners from local jail
By Ned Parker
BAGHDAD, June 17 (Reuters) - Scores of Iraqis were killed on Tuesday during a battle for a provincial capital, and fighting shut the main oil refinery, starving parts of the country of fuel and power as an uprising by Sunni insurgents threatens Iraq's survival as a state.
Government forces said they repelled an attempt by insurgents to seize Baquba, capital of Diyala province north of Baghdad, in heavy fighting overnight.
Some residents and officials said the dead included scores of prisoners from the local jail, although there were conflicting accounts of how they had died.
ISIL fighters who aim to build a Caliphate based on mediaeval Sunni precepts across the Iraqi-Syrian frontier launched their revolt by seizing the north's main city, Mosul, last week and have swept through the Tigris river valley north of Baghdad. They have boasted of massacring hundreds of troops captured in their advance.
The fighters have been joined by other Sunni factions, including former members of ousted dictator Saddam Hussein's Baath Party and tribal figures, who share widespread anger among Iraq's Sunni minority at perceived oppression by the Shi'ite-led government of Prime Minister Nuri al-Maliki.
Western countries, including the United States, have urged Maliki to reach out to Sunnis to rebuild national unity as the only way of preventing the disintegration of Iraq.
But the long-serving prime minister, who won an election two months ago, seems instead to be veering in the opposite direction - relying more heavily than ever on his own majority sect and vowing to purge opposition politicians and military officers he has labelled "traitors".
Hassan Suneid, a close Maliki ally, said on Tuesday the governing Shi'ite National Alliance should boycott all work with the largest Sunni political bloc, Mutahidoon.
"It is not possible for any bloc inside the National Alliance to work with Mutahidoon bloc due to its latest sectarian attitude," he told a TV channel of Maliki's party.
The sudden advance by Sunni insurgents is scrambling alliances in the Middle East, with the United States and Iran both saying they could cooperate against a common enemy, all but unprecedented since the 1979 Iranian revolution.
U.S. President Barack Obama, under fire at home by critics who say he did too little to shore up Iraq since withdrawing troops in 2011, is considering options for military action such as air strikes. He has sent a small number of extra marines to guard the U.S. embassy but has ruled out redeploying troops.
"The president will continue to consult with his national security team in the days to come," the White House said, without elaborating. A senior U.S. official said Obama had not yet decided on a course of action.
In a diplomatic rapprochement, Britain said it planned to reopen its embassy in Tehran, two and a half years after a mob ransacked the mission.
Officials confirmed that the Baiji refinery north of Baghdad had shut down and foreign workers were evacuated, although they said government troops still held the vast compound. With the refinery shut, Iraq will have a harder time generating electricity and pumping water to sustain its cities in summer.
The refinery has been protected by elite troops, while the nearby town largely fell to ISIL fighters last week. Baiji's refinery had stayed open despite years of civil war while U.S. forces were in the country, and the threat to it shows how much more vulnerable Iraq is now to insurgents than it was before Washington pulled out troops in 2011.
Tens of thousands of Shi'ites have rallied at volunteer centres in recent days, answering a call by the top Shi'ite cleric to defend the nation. Many recruits have gone off to train at Iraqi military bases.
But with the million-strong regular army abandoning ground despite being armed and trained by the United States at a cost of $25 billion, the government is increasingly relying on extra-legal Shi'ite militia to fight on its behalf, re-establishing groups that fought during the 2006-2007 bloodletting.
According to one Shi'ite Islamist working in the government, well-trained fighters from the Shi'ite organisations Asaib Ahl Haq, Khetaeb Hezbollah and the Badr Organisation are being deployed as the main combat force, while new civilian volunteers will be used to hold ground after it is taken.
The Sunni militants have moved at lightning speed since seizing Mosul last Tuesday, slicing through northern and central Iraq, capturing the key towns of Hawija and Tikrit in the north before facing resistance in southern Salahuddin province, where there is a large Shi'ite population.
The battle lines are now formalising, with the insurgents held at bay about an hour's drive north of Baghdad and just on the capital's outskirts to the west.
State television said Iraqi security forces repelled attacks on three neighbourhoods overnight in Baquba, capital of Diyala, an ethnically and religiously mixed province that saw some of the worst violence of the 2003-2011 U.S. occupation.
Militants also attacked a northern Iraqi village, called Basher, 15 km (9 miles) south of Kirkuk, inhabited by Shi'ite ethnic Turkmens. They were repelled, police said.
Kirkuk itself has been taken by forces from the autonomous Kurdish region. In a further sign of ethnic and sectarian polarisation, Maliki allies have accused the Kurds of colluding with Sunnis to dislodge government forces in the north.
The mainly Turkmen city of Tal Afar, west of Mosul, fell to Sunni militants late on Sunday, and the Iraqi military said it was sending reinforcement there. The Iraqi army said on state television it had killed a top militant, named Abu Abdul Rahman al-Muhajir, in Mosul in clashes.
But security officials seemed pessimistic about the situation in Mosul. One Iraqi security officer warned: "There is no clear strategy for the Iraqi government to retake Mosul. And without the US and international community support, the Iraqi government will never retake Mosul." (Editing by Peter Graff and Giles Elgood)
Our Standards: The Thomson Reuters Trust Principles.