* Militias celebrate violently after taking Bani Walid
* Former Gaddafi bastion seized by forces from rival town
* Government faces tough task of reconciling rival groups
Hours after taking control of Bani Walid, a former stronghold of Muammar Gaddafi, Libyan militias from the rival city of Misrata fired ferociously at its empty public buildings.
Fighters yelling "Allahu akbar (God is greatest) and "Today Bani Walid is finished" sought to make their mark with gunfire and rocket-propelled grenades on a town they say still provides a refuge to many of the overthrown Libyan leader's followers.
The chaotic, vengeful scenes demonstrated the weakness of the new government's authority over former rebel militias which owe it allegiance but essentially do what they like.
A sign on a bank building that bore the Gaddafi-era name for Libya, "The Great Arab Socialist People's Republic", was scarred with bullet holes. The central streets were empty except for the fighters who filled them with their violent celebration.
"The Gaddafi fighters are out of Bani Walid, they have gone," said Ali Mahmoud, a Misrata fighter in a pickup truck at a central Bani Walid roundabout, patriotic music blaring.
"Some people here still wanted Gaddafi, we have to show them that he is finished."
After days of shelling that sent thousands of families fleeing from the hilltop town in scenes reminiscent of last year's war, militias aligned with the defence ministry, a grouping known as Libya Shield, seized Bani Walid on Wednesday.
The latest fighting, in which dozens of people were killed and hundreds wounded, erupted over a government demand that Bani Walid hand over those who had kidnapped and tortured Omar Shaaban, the former rebel fighter who had caught Gaddafi hiding in a drain in his hometown of Sirte last year.
Shaaban, from Misrata, a city that underwent a harsh siege by Gaddafi's forces, died in a Paris hospital last month from injuries inflicted during two months of captivity in Bani Walid.
The United Nations had called for restraint as militias gathered menacingly around Bani Walid, whose residents had baulked at turning over the wanted men to unruly armed groups, while Libya's justice system remains in disarray.
"There are some wanted people in Bani Walid, and we do want to hand them over but they also have rights," said Murad Mohammed, a student and Warfala tribe member living in Benghazi.
"So do you expect us to give them to militias who do not have legitimacy?"
Many people in Bani Walid belong to the powerful Warfala tribe, which was mostly loyal to Gaddafi.
The town and its now-displaced inhabitants, long isolated from the rest of Libya, fear retribution and wonder what fate awaits them in the post-Gaddafi era.
A disquieting example is offered by Sirte, whose residents feel neglected by Libya's new rulers, saying they are paying the price for being the last bastion of Gaddafi, who was killed there on Oct. 20, 2011. His death has yet to be investigated.
Days later, Sirte residents were blaming vindictive rebels for some of the destruction visited on their city.
While the government has set up committees to tackle security, services and the return of refugees to Bani Walid, militia commanders say they will stay to keep the town "secure".
"Such groups have a background and a certain vision of what Libya should be and it doesn't always necessarily match that of the elected officials at the (ruling) General National Congress," said Claudia Gazzini of the International Crisis Group.
"This risks pushing back the reconciliation attempts that could have been fostered better in a peaceful manner between Bani Walid and the rest of Libya."
Bani Walid's predicament underlines the challenge Libya's new rulers face in reconciling groups with long-running grievances and embracing those who chose not to back the revolt - whether out of fear, or because they supported Gaddafi, or because they were benefiting in some way from his rule.
The government knows it must now strike the right balance and not risk storing up trouble for the future.
With 70,000 people, Bani Walid, some 170 km (105 miles) south of Tripoli, was one of the last towns to surrender to rebels last year. Gaddafi's now-captured son Saif al-Islam staged a last stand there before fleeing into the Sahara.
In January, the town grabbed headlines when fighters threw Tripoli's men out of the city, installing its own local council.
In July, fighters from Misrata threatened to attack it after two journalists from their city were detained.
While Misrata fighters have complained at what they call Bani Walid's continued defiance and its alleged harbouring of former Gaddafi loyalists, townsfolk there say they have been unfairly tarred with the "pro-Gaddafi" brush.
"Bani Walid became a centre for those who were wanted for justice to escape," government spokesman Nasser El-Manee told a news conference. "We can say that they kidnapped the city."
Rights groups have voiced concern over the conditions of some detention centres run by militias, especially in Misrata.
Some have said this month's assault amounted to revenge by Misrata, whose fighters have been quick to retaliate, sometimes brutally, against towns that have been seen as loyalist. Many of the militias attacking Bani Walid were from Misrata.
"The other problem here is that we have a military action to free men who were arbitrarily detained by Bani Walid, but at the same time we have the same situation in other cities, Misrata first of all," Gazzini said.
"I feel that the congress has sort of followed this intense lobbying attack by the Misratans and by groups of Bani Walid revolutionaries who were kicked out of Bani Walid," she said.
"Now what we are hearing from these spokesmen that they want to take the glory for this inglorious operation."
ACTS OF REVENGE
Army officials said they had freed several detainees in Bani Walid and captured some fighters who used to belong to a brigade commanded by Gaddafi's son Khamis.
Human rights groups have urged the authorities to make clear that looting, beatings and destruction will be prosecuted.
"The government and forces under its command should protect residents in Bani Walid and reject acts of revenge," said Fred Abrahams, special adviser at Human Rights Watch.
"There is an urgent need to stop destruction of the town and begin reconstruction, as well as to prosecute those who broke the law," he said in a statement.
Residents fleeing Bani Walid spoke of no water or power and little food and medicine in the town. There were unconfirmed reports of militiamen entering suburbs with bulldozers.
As in Sirte, many in Bani Walid may feel resentment, believing they have been the victims of collective punishment.
"Where is the international community?" Bani Walid tribal elder Mohammed al-Shetawi said by phone after leaving the town.
"Where is the United Nations and the European Union and the other people in the world, why have they forgotten us?"
Benghazi congress member Saleh Gaouda said the priority of the authorities was to give Bani Walid residents the security to be able to return to their homes.
"It is important to make people inside Bani Walid believe in February 17th," he said, referring to the name given to the 2011 rebellion against Gaddafi. "This can be done through the media, talking, giving them a chance to be part of society."