Shooting erupted inside the five-star Corinthia Hotel and security forces evacuated guests, including Tripoli's prime minister and an American delegation, after gunmen blasted through the building's security and reception.
It was one of the worst assaults targeting foreigners since the 2011 civil war that ousted Muammar Gaddafi and fractured the oil-producing North African state into fiefs of competing armed groups with two national governments both claiming legitimacy.
A militant group associated with Islamic State insurgents in Iraqand Syria claimed responsibility for the hotel attack as revenge for the death of a suspected Libyan al Qaeda operative in the United States, according to the SITE monitoring service.
But Tripoli officials who have set up their own self-proclaimed government blamed former Gaddafi loyalists bent on assassinating their prime minister, who was at the hotel, and said he was rescued without injury.
"The attackers opened fire inside the hotel, killing four foreigners, two men and two women, who are believed to be from East Asian countries," Omar Khadrawi, head of Tripoli security, told Reuters. "When the attackers were completely surrounded by the security forces, one of them detonated a grenade, but we don't know if it was deliberate."
A security officer was also killed in the clashes and three guards died when the attackers set off a car bomb in the car park outside the hotel.
Libya is caught in a conflict between two rival factions, one allied with the internationally recognized government, the other with "Libya Dawn" forces who took over the capital Tripoli in the summer and set up their own government.
Both claim the mantle of legitimate saviors of Libya. But in the post-revolution chaos, armed groups - from brigades of former rebels to federalist fighters and Islamist militants - have grown in power and control of territory.
Most foreign governments closed their embassies and pulled their staff out of Tripoli after factional fighting erupted last summer. But some diplomats, business and trade delegations still visit the capital.
Envoys from the United Nations, which is holding talks in Geneva with some of Libya's warring parties to end hostilities and form a unity government, also frequently visit Tripoli.
Khadrawi, the Tripoli security director, said security forces had spirited the Tripoli government's premier, Omar al-Hassi, from the 22nd floor of the hotel where he was staying.
"The attackers were attempting to assassinate him, and that is according to our investigation."
But citing social media, SITE monitors said a militant group claimed the attack as revenge for the death of Abu Anas al-Liby, a suspected al Qaeda member accused of helping plan the 1998 bombings of the U.S. embassies in Tanzania and Kenya.
Liby died in a New York hospital this month ahead of his scheduled trial.. Liby, a Libyan national, was snatched by U.S. special forces from Tripoli and taken to the United States in 2013.
Foreigners and embassies have been targeted in shootings, kidnappings and bombings in the past in Libya.
In 2012, militants attacked the U.S. Consulate in the east's main city of Benghazi, killing the U.S. ambassador and three other Americans. U.S. officials blamed a Libyan Islamist group, Ansar al Sharia, for orchestrating that attack.
Since the 2011 NATO-backed uprising that toppled Gaddafi, Libya has struggled to find stability and a conflict has gradually emerged between two loose confederations of politicians, armed groups and regional factions.
Tripoli is controlled by a faction that is allied to the city of Misrata and their powerful armed forces, but also includes some Islamist-leaning former rebel fighters and politicians allied to the Muslim Brotherhood.
They are faced by the internationally recognized government of Prime Minister Abdullah al-Thinni and the elected parliament who now operate out of the far east of the country.
They are allied with former rebels from the town of Zintan, including some ex-Gaddafi special forces who turned against the dictator. Thinni's ranks include former army general Khalifa Haftar, who has carried out his own military campaign against Islamist militants in Benghazi.