It was the first time since the civil war in neighbouring Syria erupted in 2011 that violence had spread to the market in the northern city's historical centre, which is on the shortlist for selection as a UNESCO world heritage site.
The army announced in the evening that six soldiers were killed in the fighting that rocked Tripoli and in attacks around the northern port city.
A security official said a civilian caught in the crossfire and a gunman were killed, while nine soldiers, eight civilians and six militants were wounded. One of the wounded civilians is a local journalist.
The fighting broke out late Friday after gunmen attacked an army patrol in the Khan al-Askar area, near the heart of the coastal city, wounding four soldiers, a security official said.
The gunmen then withdrew to the market's narrow alleyways and troops launched an assault on Saturday morning, setting off fierce clashes that continued into the early afternoon.
But even after the fighting ended, gunmen abducted a soldier inside the city, a security official told AFP. The army separately said it had foiled a bid to kidnap five soldiers.
Security officials said most of the gunmen involved in Saturday's fighting were Lebanese but were could not say if they were affiliated with any group.
"Some of them are Islamists, while others are wanted thugs," a security official told AFP.
An AFP journalist who toured the area after the fighting ended reported widespread damage to shops and vehicles.
The guns fell silent at around 2:00 pm (1100 GMT).
Signup to our newsletter and follow us on Facebook and Twitter!
"The army has ended its operation, having arrested a number of armed men and seized quantities of weapons and ammunition from them, while others fled and are being pursued," a statement said.
Dozens of shops were destroyed in the fighting, which included heavy shelling, said the AFP journalist who also saw a charred body and civilians evacuating the wounded on stretchers.
Elsewhere in northern Lebanon, a clash broke out in the Akkar region near the Syrian border, after unidentified gunmen tried to cut off a main road.
Two soldiers were killed in that fighting, the army said.
An officer was also killed and two others wounded in a separate attack on troops in the village of Minieh, 10 kilometres (six miles) from Tripoli, when gunmen fired rockets at their vehicle, it said.
The latest violence came nearly three months after a battle in eastern Lebanon between the army and jihadists from Syria's Al-Qaeda branch and the Islamic State (IS) group.
Dozens of civilians, including women and children, who had been trapped in the market during the fighting were able to leave when the clashes stopped.
Some were carried out by ambulances, while others left on foot.
"We are stuck between the army and the gunmen," resident Motassem al-Masri told AFP by telephone before the fighting ended.
"My brother is wounded, but the Red Cross is unable to enter the area. We are begging the army to let us out," said Masri, adding that he could see gunmen on the street.
The AFP journalist said the market area had been completely surrounded by the army, while snipers were positioned on the rooftops.
Tripoli has seen repeated clashes between Sunni militants sympathetic to the rebels in neighbouring Syria and Alawites who back the Damascus regime.
Islamist gunmen in the city have also carried out multiple attacks against the army. They accuse it of cooperating with Shiite militant group Hezbollah, which has sent thousands of fighters to Syria to support President Bashar al-Assad's forces.