Government spokesman Nasser al-Manei said the armed protesters tried to push their way into the building, some of them carrying mortars. The building was evacuated as the firefight with the security guards developed, leaving one guard dead and four wounded, he said.
Libya has been hit by lawlessness since the ouster last year of longtime dictator Moammar Gadhafi, with militiamen who fought the dictator's forces refusing to hand over their weapons.
The protesters were demanding compensation from the government.
Prime Minister Abdurrahim el-Keib was in the building meeting with his defense minister at the time of the attack, a person who was also inside said on condition of anonymity, fearing retribution. The premier was not hurt.
Cafe owner Mohammed Sabry said the militia groups descended on the prime minister's office in the morning, attacking guards and causing mayhem.
"The blood covered the sidewalk in front of my shop," he said. "Backup forces came from a nearby neighborhood to help the guards and push those people away."
Libya's transitional government has offered salaries to former fighters who helped topple Gadhafi and is trying to integrate them into a national force, so far with patchy success.
El-Keib's government has been accused of inefficiency and inability to deal key issues, like lack of security and compensation for former rebels.
Al-Manei said some of the several hundred protesters were demanding their delayed salaries. Others were demanding compensation for the wounded and more efforts to locate those missing since the civil war last year, and earlier in Gadhafi's prisons.
"We didn't stop giving salaries, but we have postponed (paying) salaries so we can apply a new mechanism that ensures no corruption is involved and no money is wasted" on fake claims, al-Manei told reporters.
He rejected the violence. "This isn't the way we negotiate things, by using force to get rights. These are the ways of Gadhafi's regime, using force against his own people."