* Saudi kings gives $1 bln to Lebanese army
* Mayor of Arsal says civilians are suffering
* Doctor says a quarter of the militants have fled
* 17 soldiers killed during incursion (Adds some militants withdraw, civilian casualties, mediation)
By Oliver Holmes and Tom Perry
BEIRUT, Aug 6 (Reuters) - The Lebanese army and Islamist militants clashed on Wednesday despite a ceasefire agreed to end five days of fighting that has killed dozens of people in the most serious spillover of Syria's three-year civil war into Lebanon.
Saudi Arabian King Abdullah granted $1 billion to help the Lebanese army to bolster security as they battle militants who have seized the border town of Arsal on the Syrian frontier, state news agency SPA reported.
Machine gun fire and shelling broke out on Wednesday morning on the outskirts of the town in breach of the 24-hour truce, which came into force at 7 p.m. (1600 GMT) on Tuesday.
"The ceasefire is continuing, but we are responding to any violations," a security official said.
A security official and a doctor in Arsal said many militants had now fled Arsal for surrounding mountains following the army bombardment.
Arsal is the first major incursion into Lebanon by hardline Sunni militants - leading players in Sunni-Shi'ite violence unfolding across the Levant - which threatens the stability of Lebanon by inflaming its own sectarian tensions.
While Lebanon has officially tried to distance itself from Syria's conflict, the country's powerful Shi'ite movement, Hezbollah, has sent fighters to aid President Bashar al-Assad. Assad, like Hezbollah, is backed by Shi'ite power Iran, Saudi Arabia's arch rival in the Gulf.
At least 17 soldiers have been killed and 22 are missing from the violence in and around Arsal. Reports from inside the town suggest dozens of people have been killed there.
Advancing Lebanese troops found the bodies of 50 gunmen on Monday, security sources said.
Arsal's mayor Ali Hujeiri, by phone, said the gunmen were on the outskirts of the town. "There was a ceasefire, but it is not being implemented," he said, adding that there appeared to be more militants in the area.
"The army is still there, the gunmen are still there, and the ones suffering are the civilians."
FIRST STOP FOR REFUGEES
The militants have been identified by officials as members of the Nusra Front, al Qaeda's branch in Syria, and of the Islamic State, which has seized large areas of Iraq and Syria.
Rebel sources told Reuters several members of the Islamic State had been killed in the Arsal fighting, including senior leader Abu Hassan al-Homsi, who had been in charge of setting up booby traps and explosions. Another leader of Jordanian origin was also killed in the fighting, the rebel sources said.
Arsal was the first stop for many civilians fleeing the bloodshed in Syria. Refugee camps in Arsal that provide shelter to tens of thousands of Syrians who fled the war have been badly damaged in the fighting, forcing them to seek shelter in the town itself, Syrian activists in the area have said.
Qassem al-Zein, a Syrian doctor at the field hospital in Arsal said militants "wanted to leave since yesterday but they haven't been able to because of the shelling."
"The important thing is to stop the shelling. The wounded and dead are still coming. Since this morning we've had 30 wounded, all from shelling and snipers. All civilians," he said.
He said the hospital had counted 36 dead civilians since the fighting started, about 10 of them people from Arsal and the rest Syrian refugees.
The clashes in Arsal began on Saturday after security forces arrested an Islamist commander popular with local rebels who often move across the porous border with Syria.
Soon after the arrest, gunmen attacked local security forces and seized the town.
The ceasefire had come into effect on Tuesday evening after the Islamists freed three policemen in what one militant called a "goodwill gesture" in response to mediation by Sunni clerics from Lebanon's Muslim Clerics' Association.
The militants in Arsal told the clerics they were willing to withdraw if the army agreed it would only man checkpoints outside Arsal and not enter the town itself.
A Lebanese political source, speaking on condition of anonymity, said the army aimed to retake the hills around Arsal. The Sunni clerics were due to enter Arsal again on Wednesday to continue talks.
Saudi news agency SPA said former Lebanese Prime Minister Saad al-Hariri, who has close links to the Saudi royal family, announced the Saudi aid after meeting King Abdullah in his summer residence in the Red Sea city of Jeddah late on Tuesday.
The king "has issued an order to provide aid to the Lebanese army and the national security (forces) to the value of $1 billion to support their ability to maintain the security and stability of Saudi Arabia's sister country, Lebanon", said Hariri, cited by SPA.
Lebanon - a country of about 4 million, bordering Israel - has avoided the kind of war afflicting Syria and Iraq, but regional conflicts have rekindled decades-old tensions.
Rocket fire, suicide attacks and gun battles connected to Syria's war have plagued Lebanon and the conflict has worsened Lebanon's perennial political deadlock, with officials divided largely along sectarian lines.
More than 170,000 people have been killed in Syria's war, which started in 2011 as a peaceful protest movement, then degenerated into civil war after a government crackdown. (Additional reporting by Alexander Dziadosz in Beirut and Amena Bakr in Doha; Editing by Will Waterman)