UPDATE 4-Gunmen attack headquarters of Libya's state oil firm, two staff killed
Security forces said they had regained control of the landmark glass-fronted building in the centre of the city.
The attack came less than a week after a fragile truce halted fierce clashes between rival armed groups in Tripoli, the latest eruption of violence in Libya, which has been in turmoil since a 2011 uprising.
Armed groups regularly block oilfields to make demands but the NOC headquarters had so far been spared the violence engulfing the North African country.
Gunfire rang out in the morning as security forces allied to the Tripoli-based government arrived.
"The death toll so far is two killed from NOC staff and two attackers," said Ahmad Ben Salim, spokesman for the Special Deterrence Force (Rada), one of the most powerful factions in Tripoli.
Security forces smashed windows so staff could escape and several people were hurt by shattered glass, witnesses said. NOC Chairman Mustafa Sanalla and his office manager were seen safely leaving the building.
Libya has been divided between rival governments and military factions based in the east and west of the country since 2014, causing political deadlock and an economic crisis.
Oil production has been hit by attacks on oil facilities and blockades, though last year it partially recovered to around one million barrels per day (bpd).
Islamist militants have sleeper cells in northern cities as well as mobile units in Libya's southern desert, according to Libyan and Western officials.
The NOC attack came a day after the United Nations had said the major armed factions had agreed to freeze their positions to extend the ceasefire.