BEIRUT — Lebanon's third-largest city of Sidon was turned into a battle zone Monday as the military fought heavily armed followers of an extremist Sunni Muslim cleric holed up in a mosque.

Residents of the southern port fled machine-gun fire and grenade explosions that shook the coastal area in one of the deadliest rounds of violence, seen as a test of the weak government's ability to contain the furies unleashed by the civil war in neighboring Syria.

Official reports said at least 16 soldiers were killed and 50 were wounded in two days of clashes with armed followers of Ahmad al-Assir, a maverick Sunni sheik whose rapid rise is a sign of the deep frustration among many Lebanese who resent the ascendancy of Shiites to power, led by the militant group Hezbollah. More than 20 of al-Assir's supporters were killed, according to a security official who spoke on condition of anonymity because he wasn't authorized to talk to reporters.

The fierce battle that al-Assir's fighters were putting up showed how aggressive Sunni extremists have grown in Lebanon, building on anger not only at Syria's regime but also its allies in Hezbollah.

"Sidon is a war zone," said Nabil Azzam, a resident who returned briefly Monday to check on his home after having fled with his family a day earlier. "This is the result of all the sectarian rhetoric that has been building because of the war in Syria. It was bound to happen," he said by telephone, a conversation interrupted by a burst of gunfire.

Machine-gun fire and explosions from rocket-propelled grenade caused panic among residents, who also reported power and water outages. Snipers allied with al-Assir took over rooftops, terrorizing civilians, and many were asking to be evacuated from the heavily populated neighborhood around the Bilal bin Rabbah Mosque, where al-Assir preaches and where the fighting has been concentrated.

The military appealed to the gunmen to turn themselves in, vowing to continue its operations "until security is totally restored." By evening, the army had stormed the mosque complex, though not the mosque itself.

In addition to the more than 20 followers of the cleric who were killed, dozens of them were arrested, the security official said. There was no sign of al-Assir and it was unclear if he was in the mosque or had managed to escape.

The fighting in Sidon is the bloodiest involving the army since the military fought a three-month battle in 2007 against the al-Qaida-inspired Fatah Islam group inside the Palestinian refugee camp of Nahr el-Bared in northern Lebanon. The Lebanese army crushed the group, but the clashes killed more than 170 soldiers.

The scenes of soldiers aiming at gunmen holed up in residential buildings and armored personnel vehicles deployed in the streets evoked memories of Lebanon's 1975-90 civil war.

The challenges facing the Lebanese military resemble those that prevailed in that conflict, which eventually splintered the army along sectarian lines.

"It's the memory of this destructive war that remains as a restraining force — for now," said Fawaz A. Gerges, director of the Middle East Center at the London School of Economics.

Syria's civil war has been bleeding into Lebanon for the past year, following similar sectarian lines of Sunni and Shiite camps. Overstretched and outgunned by militias, the military has struggled on multiple fronts in the eastern Bekaa valley and the northern city of Tripoli as armed factions fought street battles that often lasted several days.

In many cases, soldiers stood by helplessly and watched the violence.

On Monday, however, the army moved against al-Assir after his followers opened fire on an army checkpoint unprovoked.

Al-Assir, a 45-year-old bearded cleric who supports the overwhelmingly Sunni rebels fighting to topple Syrian President Bashar Assad, is an unlikely figure to challenge the Lebanese army.

Few had heard of him until last year, when he began agitating for Hezbollah to disarm, taking advantage of the deep frustration among Lebanon's Sunnis and a political void on the Sunni street following the 2005 assassination of former Prime Minister Rafik Hariri, a powerful Sunni leader.

Last year, al-Assir set up a protest tent city that closed a main road in Sidon for a month in a sit-in meant to pressure Hezbollah to disarm.

He kept local and international media entertained by pulling stunts such as riding his bicycle and getting his hair cut in public while he openly challenged and taunted Hezbollah like few had dared before. He even publicly criticized Hezbollah leader Sheik Hassan Nasrallah — something few would do in Lebanon.

In February, al-Assir caused a stir when he and hundreds of his bearded supporters arrived in buses at a ski resort in the Christian heartland, where residents set up roadblocks to try to keep him out.

He teamed up with Fadel Shaker, a once-prominent Lebanese singer-turned Salafist, who took to reciting verses of the Quran at al-Assir's protests. Shaker's brother, a close aide to al-Assir, was killed in confrontations with the army Monday, the National News Agency said.

Despite his attention-seeking tactics, al-Assir's rants against Hezbollah resonated with many Sunnis who are bitter about Hezbollah's increasingly dominant role in Lebanese politics.

Many in the Western-backed coalition known as March 14, headed by Hariri's son, Saad, quietly backed al-Assir as he launched his anti-Hezbollah tirades, and several Sunni politicians attacked the army, accusing it of bias in favor of Hezbollah.

Last month, after Hezbollah openly joined Assad's forces in the border town of Qusair, al-Assir called on Sunnis in Lebanon to enter the fight in in Syria, and posted pictures of himself allegedly in Qusair before its fall into government hands. He accused the army of inaction in the face of Hezbollah's growing involvement in Syria.

But al-Assir appears to have overplayed his cards by attacking the army, the only trusted institution in the country, triggering a backlash.

"The bravery of the army facing al-Assir's well-armed supporters has shamed Lebanese politicians," said Hisham Jaber, a retired army general who heads a Beirut-based think tank. He said the army appeared determined to remain neutral despite attempts by politicians to splinter it.

Sidon, located about 40 kilometers (25 miles) south of Beirut, has largely been spared the violence plaguing border areas. The clashes began Sunday in the Mediterranean city after troops arrested an al-Assir follower. The army says the cleric's supporters opened fire without provocation on an army checkpoint.

Many people living on upper floors moved downstairs for cover or fled to safer areas. Some were seen carrying children. Others stayed locked in their homes or shops, afraid of getting caught in the crossfire. Gray smoke billowed over parts of the city.

Hezbollah appeared to be staying largely out of the clashes, although a few of its supporters in Sidon were briefly drawn into the fight Sunday, firing on al-Assir's supporters. At least one was killed, according to his relatives in the city who spoke on condition of anonymity because they feared for their safety.

Last week, al-Assir supporters fought with pro-Hezbollah gunmen, leaving two dead.

Fighting also broke out in parts of Ein el-Hilweh, a teeming Palestinian refugee camp near Sidon, where al-Assir has supporters. Islamist factions in the camp lobbed mortar rounds at military checkpoints around the camp.

Tension also spread to the north in Tripoli, Lebanon's second-largest city. Masked gunmen roamed the city center, firing in the air and forcing shops and businesses to shut down in solidarity with al-Assir. Dozens of gunmen also set fire to tires, blocking roads. The city's main streets emptied out, but there was no unusual military or security deployment.

"The Syrian fire is beginning to devour Lebanon, and the longer the conflict goes on, the more danger there is for Lebanon to implode," Gerges said.

Syrian Foreign Minister Walid al-Moallem blamed the violence in Lebanon on the international decision to arm the rebels, saying that it will only serve to prolong the fighting in Syria and will affect Lebanon.

"What is going in Sidon is very dangerous, very dangerous," he told reporters in Damascus. "We warned since the start that the impact of what happens in Syria on neighboring countries will be grave."