(Adds evacuation of oil workers, Saudi comment, Maliki)
* Rebels control most of Baiji refinery
* Sunni fighters have seized major cities
* Fighting threatens to spill over borders
By Ghazwan Hassan
BAGHDAD, June 18 (Reuters) - Sunni rebels battled their way into the biggest oil refinery in Iraq on Wednesday, and the president of neighbouring Iran raised the prospect of intervening in a sectarian war that threatens to sweep across Middle East frontiers.
Sunni fighters were in control of three quarters of the territory of the Baiji refinery north of Baghdad, an official said there, after a morning of heavy fighting at gates defended by elite troops who have been under siege for a week.
A lightning advance has seen Sunni fighters rout the Shi'ite-led government's army and seize the main cities across the north of the country since last week.
The fighters are led by the Islamic State of Iraq and the Levant which aims to build a Sunni caliphate ruled on mediaeval precepts, but also include a broad spectrum of more moderate Sunnis furious at what they see as oppression by Baghdad.
Some international oil companies have pulled out foreign workers. The head of Iraq's southern oil company, Dhiya Jaffar, said Exxon Mobil had conducted a major evacuation and BP had pulled out 20 percent of its staff. He criticised the moves, as the areas where oil is produced for export are mainly in the Shi'ite south and far from the fighting.
Washington and other Western capitals are trying to save Iraq as a united country by leaning hard on Shi'ite Prime Minister to reach out to Sunnis. Maliki met Sunni and Kurdish political opponents overnight, concluding with a frosty, carefully staged joint appearance at which an appeal for national unity was read out.
In a televised address on Wednesday Maliki appealed to tribes to renounce "those who are killers and criminals who represent foreign agendas".
But so far Maliki's government has relied almost entirely on his fellow Shi'ites for support, with officials denouncing Sunni political leaders as traitors. Shi'ite militia - many believed to be funded and backed by Iran - have mobilised to halt the Sunni advance, as Baghdad's million-strong army, built by the United States at a cost of $25 billion, crumbles.
Like the civil war in Syria next door, the new fighting threatens to draw in regional neighbours, mustering along sectarian lines in what fighters on both sides depict as an existential struggle for survival based on a religious rift dating to the 7th Century.
Iran's President Hassan Rouhani made the clearest declaration yet that the Middle East's main Shi'ite power, which fought a war against Iraq that killed a million people in the 1980s, was prepared to intervene to protect Iraq's great shrines of Shi'ite imams, visited by millions of pilgrims each year.
"Regarding the holy Shi'a shrines in Karbala, Najaf, Kadhimiya and Samarra, we announce to the killers and terrorists that the great Iranian nation will not hesitate to protect holy shrines," Rouhani said in an address to a crowd on live TV.
He said many people had signed up to go to Iraq to fight, although he also said Iraqis of all sects were prepared to defend themselves: "Thanks be to God, I will tell the dear people of Iran that veterans and various forces - Sunnis Shias and Kurds all over Iraq - are ready for sacrifice."
Iraqi troops are holding off Sunni fighters outside Samarra north of Baghdad, site of one of the main Shi'ite shrines. The fighters have vowed to carry their offensive south to Najaf and Kerbala, seats of Shi'ite Islam since the Middle Ages.
Saudi Arabia, the region's main Sunni power, said Iraq was hurtling towards civil war. Foreign Minister Prince Saud al-Faisal, in words clearly aimed at Iran and at Baghdad's Shi'ite rulers, deplored the prospect of "foreign intervention" and said governments need to meet "legitimate demands of the people".
Maliki's government has accused Saudi Arabia of promoting "genocide" by backing Sunni militants. Riyadh supports Sunni fighters in Syria but denies aiding ISIL.
The Baiji refinery is the fighters' immediate goal, the biggest source of fuel for domestic consumption in Iraq, which would give them a firm grip on energy supply in the north where the local population has complained of fuel shortages.
The refinery was shut on Tuesday and foreign workers flown out by helicopter.
"The militants have managed to break in to the refinery. Now they are in control of the production units, administration building and four watch towers. This is 75 percent of the refinery," an official speaking from inside the refinery said.
The government denied the refinery had fallen. Counter-terrorism spokesman, Sabah Nouri, insisted forces were still in control and had killed 50 to 60 fighters and burned 6 or 7 insurgent vehicles after being attacked from three directions.
Last week's sudden advance by ISIL - a group that declares all Shi'ites to be heretics deserving death and has proudly distributed footage of its fighters gunning down prisoners lying prone in mass graves - is a test for U.S. President Barack Obama, who pulled U.S. troops out of Iraq in 2011.
Obama has ruled out sending back ground troops but is considering other military options to help defend Baghdad, and U.S. officials have even spoken of cooperating with Tehran against the mutual foe.
But U.S. and other international officials insist Maliki must do more to address the widespread sense of political exclusion among Sunnis, the minority that ran Iraq until U.S. troops deposed dictator Saddam Hussein after the 2003 invasion.
Western countries fear an ISIL-controlled mini-state in Syria and Iraq could become a haven for militants who could then stage attacks around the globe.
British prime minister David Cameron told parliament he disagreed "with those people who think this is nothing to do with us and if they want to have some sort of extreme Islamist regime in the middle of Iraq it won't affect us. It will.
"The people in that regime, as well as trying to take territory, are also planning to attack us at home in the United Kingdom," Cameron said.
In a rerun of previous failed efforts at bridging sectarian and ethnic divisions, Shi'ite, Sunni and Kurdish leaders met late Tuesday behind closed doors. They later stood frostily before cameras as Ibrahim al-Jaafari, a Shi'ite politician who held the post of prime minister before Maliki, read a statement.
"No terrorist powers represent any sect or religion," Jaafari said in the address, which included a broad promise of "reviewing the previous course" of Iraqi politics. Afterwards, most of the leaders, including Maliki and Usama al-Nujaifi, the leading Sunni present, walked away from each other in silence.
Though the joint statement said only those directly employed by the Iraqi state should bear arms, thousands of Shi'ite militiamen have been mobilised to defend Baghdad.
According to one Shi'ite Islamist working in the government, well-trained organisations Asaib Ahl Haq, Khataeb Hezbollah and the Badr Organisation are now being deployed alongside Iraqi military units as the main combat force.
With battles now raging just an hour's drive north of the capital, Baghdad is on edge. The city of 7 million people saw fierce sectarian street fighting from 2006-1007 and is still divided into Sunni and Shi'ite districts, some protected by razor wire and concrete blast walls.
India said it was worried about 40 Indian construction workers missing in territory seized by ISIL. (Reporting By Ghazwan Hassan, Ahmed Rasheed, Ned Parker; Editing by Giles Elgood)