“Bani Walid is free!” shouted pro-Libyan government forces as they took control of the former Gaddafi stronghold.
Commanders said the town, which has been the scene of weeks of fighting, was one of the last to surrender following last year’s civil war.
Recent clashes have killed dozens of people and injured hundreds, according to the state news agency.
“We have now liberated Bani Walid completely from Gaddafi’s remaining forces,” said one fighter, “and all areas of the town have been neutralised completely.”
Pro-government troops stepped up their assault on the town following the death in jail of Omran Shaban, the fighter who found former dictator Muammar Gaddafi hiding in a sewer last October.
“Our mission was to secure Bani Walid so that families can return. We will now return the town to the interior ministry’s control and leave,” said Field Commander Mousa al Asmar.
Thousands of residents have fled the violence.
The Red Cross estimates that around a third of the population are now living in refugee camps outside the town.
Those that have stayed complain there is no water or electricity and that food and medicine are in short supply.