BANI WALID, Libya — Libya’s government declared Wednesday that it had taken control of one of the last strongholds of deposed dictator Moammar Khadafy’s loyalists, as its fighters in the heart of the city fired their guns into the air to celebrate victory after fierce battles that left dozens dead and thousands displaced.
The capture of Bani Walid, some 90 miles southeast of Tripoli, was a triumph for the government that replaced Khadafy’s regime. But the length of time it took the government to secure the town — a full year — underlined the difficulties the new regime faces in imposing its authority over squabbling tribes and heavily armed militias.
The victory could even spark new violence. The government-backed militia that led the charge came from Misrata, a longtime rival of Bani Walid, and reprisals could result.
The military’s chief of staff, Youssef al-Mankoush, said that military operations in the city were terminated but that some forces were still chasing a few pockets of Khadafy loyalists. He was speaking in Tripoli.
In the center of Bani Walid, shops were closed and streets deserted. A power station was destroyed, the main hospital was not functioning, and a doctor was among the wounded. Fighters opened fire on street signs that bore language associated with Khadafy’s regime. A portrait of the slain dictator had its face punched out with bullet holes.
‘‘Bani Walid is under full control,’’ the official LANA news agency quoted the spokesman of the progovernment militia, Mohammed al-Kandouz, as saying late Tuesday.
But Mohammed al-Taib, a commander of a progovernment militia called Libya Shield, said that even though his forces control the town center, there was still fighting going on elsewhere. Columns of smoke billowed near the airport, where militiamen said they were meeting resistance.
Omar Boughdad, a commander from the Misrata militia, said his forces would remain in the town to keep Khadafy loyalists out.
‘‘The loyalists have fled to the valleys, but we will clean up these places, and we will not leave again,’’ Boughdad said.
Bani Walid became a bastion of Khadafy loyalists during and after the eight-month civil war that led to Khadafy’s killing and the fall of his regime last year.
It was occupied by anti-Khadafy rebels who negotiated an entry after the leader’s October death, but fighters loyal to the ousted regime rose up and pushed them out in January.
The Libyan government’s new offensive came in the aftermath of the kidnapping, torturing, and killing last month of an anti-Khadafy fighter, allegedly by residents of the town.
The country’s newly elected General National Congress issued a decree to arrest the men accused of the killing. Efforts for a peaceful resolution through negotiations with the town’s elders failed.
Government spokesman Misrata al-Manei said 50 people on the government side were killed and hundreds of others wounded in the Bani Walid operation. Manei, speaking in Tripoli, said about 100 of those wanted by the government were arrested, while 13 civilians held by the fighters in Bani Walid were freed.
Mohammed al-Harari, minister of local administration, said 10,300 families were displaced by the fighting.
Interim President Mohammed El-Megarif expressed support for the offensive on Bani Walid in a speech aired on national TV.
‘‘This is not targeting a region, a tribe, or unarmed civilians, but outlaws,’’ Megarif said. ‘‘This is to impose state legitimacy.’’