Libyan militia attacked white-flag-carrying protesters demanding the disbanding of the country's rampant armed groups on Friday, killing at least 31 people as they opened fire on the march with heavy machine guns and rocket-propelled grenades.

Libyan prime minister Ali Zeidan blamed the protesters and the militia alike for the violence, though witnesses said they saw no protesters carrying weapons before the shooting on Friday afternoon. By Friday night, however, some protesters joined by other militias had armed themselves and heavy gunfire rang out in the Tripoli neighbourhood where the attack happened.

The march in the capital by thousands of protesters was the biggest show of public anger at militias in months. Since the 2011 fall of Muammar Gaddafi, hundreds of militias – many of them on government payroll – have run out of control in Libya, carving out zones of power, defying state authority and launching violent attacks.

The protesters marched from a central mosque to the headquarters of a militia originally from the city of Misrata that has a powerful presence in Tripoli. They waved Libyan and white flags and chanted, "We want an army, we want police," referring to demands that the country's weak security forces take the place of militias.

When they neared the building, militiamen in civilian clothes and military uniforms came out of the headquarters, opening fire at the protesters with automatic weapons, RPGs and anti-aircraft guns. Protesters ran from gunfire while carrying others covered in blood.

Libya's state television put the death toll at 31, with 235 people wounded.

Witnesses all said the protesters carried no weapons. Al-Taher Basha Agha, commander of Misrata-based militia, told Libya's private al-Ahrar television station that the protesters were armed and opened fire first.

Asked if he would leave Tripoli, the commander said that his men will leave only "dead bodies".

"Tripoli has not seen a war yet, it will see it soon," he said.

Zeidan also blamed both protesters and the militiamen for the violence in a televised news conference.

"You can't open fire at people who are exchanging fire," Zeidan said.

Protesters said they were shocked by the prime minister's comments, pointing to an image of a bleeding elderly man shared on social media.

"This is delusional," protester Abdel-Karim al-Beriki said. "The first martyr was a man in his seventies. How could he be carrying a weapon?"

Libya's militias grew out of the informally created local brigades of rebels who battled Gaddafi's military. Since his fall and death, the militias have mushroomed in number, size and power. With the army and police still weak, the government has turned to militias to keep security, giving them tasks guarding facilities or districts. But the government pay has not put them under state control, and the armed groups – some of which include Islamic militants – act on their own agendas. Many of them were engaged in kidnappings, torture, assassinations and taking the law into their own hands.

The government has put a December deadline on groups to join state security forces or face losing their government paychecks though it is not clear if the government will carry out the threat, since it could spark a powerful backlash. It has made similar threats in the past.

Many militias have turned villas and residential compounds of former Gaddafi-era officials into camps where they stash weapons and impose control over certain areas. Eastern militias also have seized control of oil exporting terminals, sending production plunging from 1.4m barrels a day to around few hundred thousand, robbing the country of its main revenue source.

Friday's march was prompted by a string of incidents involving militias most recently, street clashes between the Misrata militia and one from Tripoli. The fight was sparked by the killing of one of the Misrata group's commanders, and the gun battles in the street panicked residents.

Al-Sadat al-Badri, the head of Tripoli's city council, called for three-day mourning in the capital while urging residents for "self-restraint."

The reaction was reminiscent of a similar scene last year in the eastern city of Benghazi, where thousands of protesters besieged headquarters of Islamic militias, forcing them to flee and clashed with others where dozens were killed. The protests came days after the killing of US ambassador Chris Stevens and three other Americans in a deadly attack on an American mission in Benghazi.

On Friday, the US state department said it had been quietly offering rewards since January of up to $10m for information leading to the arrest or conviction of any person involved in the Benghazi attack.