Gunmen have attacked a security forces headquarters in Libya's eastern city of Benghazi, killing nine people and wounding 24, authorities said, blaming a group allegedly behind the attack of a US diplomatic post there.

A security official said Friday's attack started when dozens of gunmen opened fire with machine guns and mortar bombs. The attack lasted for an hour, with the fighting heard miles away.

Libyan commandos later arrived and fought the attackers, reported the Associated Press news agency. The official said the attackers suffered heavy casualties, the agency added.

A statement issued by the interim government and read by Ahmed al-Amin, the cabinet spokesman, put the death toll at nine people.

Milad al-Zowi, a commando spokesman, said the dead were six army commandos and three police officers.

Zowi said that three soldiers and a police officer were missing after the battle.

A hospital official said his hospital treated 24 people wounded in the fighting, with most suffering gunshot wounds to the chest and the abdomen. Some were in critical condition, he said.

The government said a number of fighters were killed, while others were wounded and arrested. 

Attackers likely tried to get their hands on a car loaded with weapons and ammunition that the security forces had confiscated the previous night, authorities added.

A security official at the Benghazi headquarters, Gamal al-Amami, said the driver of the vehicle belonged to the Libya Shield militia.

The government's statement blamed the Ansar al-Sharia group for the attack, along with other "criminal groups", adding it would not tolerate "the presence of armed and illegal terrorist groups".

Ansar al-Sharia is blamed for the attack on the US diplomatic mission that killed the US ambassador, Chris Stevens, and three other Americans on September 11, 2012.

Source: AP