By Mariam Karouny and Tom Perry

BEIRUT (Reuters) - The Lebanese army and Islamist militants agreed a 24-hour ceasefire on Tuesday after four days of fighting triggered by the rebels' seizure of a border town, in the most serious spillover of Syria's three-year civil war into Lebanon.

A security source said the ceasefire would allow time for a mediator to investigate the fate of 22 soldiers missing since the militants seized the town of Arsal on Saturday and to help evacuate civilians, including those wounded in the conflict.

"It is like a humanitarian ceasefire," he told Reuters. The ceasefire, which came into force at 7 p.m. (12.00 p.m. EDT), was broken briefly late in the evening when an army position came under fire but calm quickly returned, the source added.

"Clashes erupted but now they have ended. The ceasefire is still on, it did not collapse. What happened was to be expected due to differences between the fighters," he said

The source said the militants had suffered big losses in the fighting, adding he expected them to leave Arsal before the ceasefire had ended.

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.

At least 17 Lebanese soldiers have been killed in the last few days of violence in and around Arsal, though there has been no sign of the conflict spreading to other border towns.

It is unclear how many militants and civilians have been killed, though death tolls given by security officials and a doctor indicate it is in the dozens.

POLICEMEN RELEASED

Earlier on Tuesday, the Islamists released three policemen they had been holding, in what one militant described as a goodwill gesture in response to a mediation by Sunni clerics from the Muslim Clerics' Association.

Story continues

The militants in Arsal told the clerics they were willing to withdraw if the army agreed to return only to man checkpoints outside Arsal and not to enter the town itself.

A Lebanese political source, speaking on condition of anonymity, said the army aimed to retake the hills around Arsal.

"It is not proposed for it to take the battle to the heart of the town," the political source said. 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.

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.

Some Sunni Muslims joined Syrian rebels and the powerful Shi'ite group Hezbollah has sent fighters to aid President Bashar al-Assad, an Alawite, who also has the backing of Iran.

Hezbollah denied it was involved in the battles in Arsal.

"Confronting the terrorist gunmen is the responsibility of the Lebanese Army solely. Hezbollah did not intervene in what happened or is happening in Arsal," it said in a statement.

Rocket fire, suicide attacks and gunbattles connected to Syria's war have plagued Lebanon and the conflict has worsened Lebanon's perennial political deadlock between officials divided largely along sectarian lines.

In the northern Lebanese city of Tripoli, which has seen frequent clashes between local Sunnis and members of the Shi'ite-derived Alawite minority, men blocked several roads on Tuesday. Most shops were shut and streets empty after militants opened fire on a bus carrying soldiers, wounding at least six.

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.

Syrian activists and medics in Arsal say fighting has badly damaged the camps that are home to many of the tens of thousands of Syrian refugees estimated to live in and around the town.

"The situation is bad. Families are blockaded inside the city. Refugees are on the streets. There is a severe shortage of bread. The medical situation is very bad," a Syrian witness told Reuters in a text message.

(Additional reporting Alexander Dziadosz; Editing by Gareth Jones)