* Soldiers came under heavy fire in Tikrit on Tuesday
* Speaker elected but caretaker PM Maliki's future uncertain
* Govt forces repel militants from town north of Baghdad
By Raheem Salman and Maggie Fick
BAGHDAD, July 16 (Reuters) - Iraqi forces have withdrawn from the militant-held city of Tikrit after their new offensive met heavy resistance, in a blow to the government effort to push back Sunni insurgents controlling large parts of the country.
The failure highlights the difficulties of Baghdad's struggle to recapture territory from the insurgents who seized Mosul, Tikrit and other cities last month in a rapid offensive which threatens to fragment Iraq on ethnic and sectarian lines.
The setback came as Iraqi politicians named a moderate Sunni Islamist as speaker of parliament on Tuesday. That was a long-delayed first step towards a power-sharing government urgently needed to confront the militants, who are led by the al Qaeda offshoot Islamic State.
It is unclear if the election of Salim al-Jabouri as speaker will break the broader deadlock over Prime Minister Nuri al-Maliki's bid to serve a third term. He has ruled since the April election as a caretaker.
Government troops and allied Shi'ite volunteer fighters retreated from Tikrit before sunset on Tuesday to a base four km (2.5 miles) south after coming under heavy mortar and sniper fire, a soldier who fought in the battle said.
Residents said there was no fighting on Wednesday morning in Tikrit, which lies 160 km (100 miles) north of Baghdad. It is a stronghold of ex-army officers and loyalists of executed former dictator Saddam Hussein's Baath Party who allied themselves with the Islamic State-led offensive last month.
Tuesday's military attack was launched from Awja, Saddam's birthplace some 8 km (5 miles) south of the city, but ran into heavy opposition in the southern part of the city.
Pictures published on Twitter by supporters of the Islamic State showed a fighter holding a black Islamist flag next to a black armoured car it said had been abandoned by a military SWAT team, as well as vehicles painted in desert camouflage - one of them burnt out - which it said retreating troops left behind.
MALIKI'S FUTURE UNCLEAR
The stunning advance in the north and west by the militants over the past month has put Iraq's very survival in jeopardy, as its politicians remain divided over forming a government to confront the insurgency.
A shared resentment at Maliki's style of rule, which his critics say has marginalised Iraq's Sunnis and Kurds, may have bolstered the Islamic State's offensive last month.
The Shi'ite leader has defied demands from Sunnis and Kurds that he step aside for a less polarising figure. He also faces challenges from within the National Alliance, a Shi'ite umbrella group that includes Maliki's State of Law bloc and rivals.
After quickly picking Salim al-Jabouri as speaker on Tuesday afternoon, lawmakers argued bitterly for hours over his Shi'ite deputy, suggesting they are still far from a deal to complete the formation of a new government or a decision on the fate of Maliki.
Now that parliament has picked a speaker, it has 30 days to elect a president, who will then have 15 days to nominate a prime minister.
Since Iraq's post-Saddam constitution was adopted in 2005, the prime minister has always been a member of the Shi'ite majority, the speaker a Sunni and the largely ceremonial president a Kurd. Each of the three is meant to have two deputies, drawn from the other two groups.
An official from Maliki's State of Law bloc said the group understood that it would not be able to push through Maliki's nomination for a third term and there was a need for "transition", but did not say how that might come about.
Maliki has given no indication he is willing to step aside.
"Prime Minister Maliki can't and shouldn't be pushed out," the official told Reuters, adding there had been no formal discussion yet on possible nominees for prime minister.
In the town of Dhuluiya southeast of Tikrit, where Sunni tribesmen and local police have been fighting militants for days, government forces sent from the city of Samarra pushed the militants out on Tuesday night, eyewitnesses said.
Islamic State gunmen had overrun government offices on Sunday morning and tried to take the main police station, local police and eyewitnesses said. The town is 70 km (45 miles) north of Baghdad.
Residents escaped the fighting by boat on the River Tigris after militants bombed the bridge and blocked off roads leading out of the town. The destruction of the bridge also blocked the sending of reinforcements from the military base near the Shi'ite town of Balad, across the river.
Control of Dhuluiya has passed several times from local fighters and police into the hands of militants and back again. (Additional reporting by a reporter in Salahuddin province, Ned Parker and Dominic Evans; Writing by Maggie Fick; Editing by Dominic Evans and Andrew Roche)