Ten other people were wounded in what the foreign ministry said was a suicide attack carried out by "terrorists".
A car bomb exploded near the ministry, prompting security forces to rush to the scene, said special forces spokesman Tarak al-Dawass, accusing the Islamic State group (IS) of responsibility.
A suicide bomber then blew himself up on the second floor of the building while a second attacker died when the suitcase he was carrying exploded, he said.
A third assailant, who was unarmed and wearing a bulletproof vest, was killed by security forces outside, Dawass added.
At least three people were killed and 10 wounded, according to the health ministry.
A civil servant who headed a department in the foreign ministry was among the dead, security sources said.
Libya in chaos since 2011 overthrow of Gaddafi
Plumes of smoke were seen rising from the building as ambulances, paramedics and security forces gathered outside.
There was no immediate claim of responsibility.
Torn apart by power struggles and undermined by chronic insecurity, Libya has become a haven for militants since the ouster and killing of Moamer Kadhafi in 2011.
Two competing administrations, rival militias, tribes and militants have been competing for control of territory and the country's vast oil wealth.
IS took advantage of the chaos to gain a foothold in the coastal city of Sirte in 2015.
Forces loyal to the UN-backed Government of National Accord (GNA) regained control of the city in December 2016 after eight months of deadly fighting.
Since then, some militants have returned to the desert in an attempt to regroup and reorganise.
In September, IS claimed responsibility for a suicide attack on the headquarters of Libya's National Oil Company in the heart of Tripoli which left two dead and 10 wounded.
Four months earlier, it claimed an attack on the electoral commission's headquarters which left 14 dead.
In April, the GNA launched an operation to track down IS fighters operating in areas of western Libya under its control.
Last month IS claimed responsibility for an attack on militia forces in southeastern Libya in which at least nine people were killed.
The US military has regularly carried out strikes on militants in Libya, particularly south of Sirte.
The GNA was set up under a 2015 UN-brokered deal, but a rival administration based in the country's east aligned with military strongman Khalifa Haftar refuses to recognise its authority.
Rival Libyan leaders had agreed to a Paris-brokered deal in May to hold a nationwide election by the end of the year.
But instability, territorial disputes and divisions have delayed plans for elections.
Two days of meetings last month in Italy laid bare deep divisions between key players in the crisis with some delegates refusing to sit side by side.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ