TRIPOLI, Libya — Militants claiming allegiance to the Islamic State said they were responsible for an armed assault on a luxury hotel that killed at least eight people here on Tuesday, the most significant in a string of terrorist attacks against Western interests and government institutions in the capital since the ouster of Col. Muammar el-Qaddafi more than three years ago.

Four or five gunmen shouting “God is great” stormed the hotel, the Corinthia, in the early morning, witnesses said. The attackers fired their guns into the lobby, battling guards and indiscriminately shooting at civilians, according to witnesses, news reports and people in contact with associates inside the hotel.

Officials said at least five of those killed were foreign visitors. The State Department confirmed that an American had been killed, and witnesses said he was a former Marine working in security. The Associated Press, citing an executive at Crucible, a security company in Fredericksburg, Va., later identified the American as David Berry, a contractor with Crucible.

It was the deadliest attack on Western interests in Libya since the assault on the American diplomatic mission in Benghazi that killed Ambassador J. Christopher Stevens in 2012. It was also the bloodiest of several recent attacks by Libyans pledging loyalty to the Islamic State, raising fears that its example was further inflaming Libya’s violence.

Fighters wearing black uniforms labeled “police” and loyal to the Tripoli government — one of two rival governments now fighting for control of Libya — responded to the attack, cordoning off streets and surrounding the hotel. Their forces entered a long standoff with assailants still inside.

A car exploded in the hotel parking lot, although it was unclear whether the cause was a bomb, a rocket-propelled grenade or some kind of missile. Local officials said later that at least one of the assailants had killed himself with a grenade or a suicide vest.

The hotel, one of the most luxurious in Tripoli, the capital, is a hub for foreign tourists and businesspeople visiting Libya, and it also houses the offices of several foreign embassies. But most foreigners have fled as the country has descended into chaos and armed conflict since last summer. Libyans who do business in the hotel said it was largely empty when the attack began.

Image

Libyan security forces near the Corinthia hotel in Tripoli on Tuesday, after assailants killed at least five people there in an attack.CreditMahmud Turkia/Agence France-Presse — Getty Images

An unidentified hotel employee told The Associated Press that guests, including British, Italian and Turkish visitors, had fled out the back as the attackers entered the lobby. There were initial reports that some of the attackers had taken hostages. By midday, however, security officials interviewed on Libyan television said that there were no hostages and that at least two of the attackers had been killed.

A group calling itself the Tripoli Province of the Islamic State, the extremist group that has seized territory in Syria and Iraq, issued a statement on social media taking responsibility for the attack just as it was beginning, and the timing added credibility to the claim. The group portrayed the assault as retaliation for the abduction last year by American commandos of a Libyan Qaeda operative, Nazih Abdul-Hamed al-Ruqai, also known as Abu Anas al-Libi.

Mr. Ruqai, 50, died this month in a New York hospital of complications from liver surgery as he was waiting to stand trial for a role in the Qaeda bombings of the American Embassies in Kenya and Tanzania in 1998.

Accompanying a picture of smoke rising from the hotel parking lot, the statement called the attack “an inside operation” by “the heroes of the caliphate,” and called the hotel a “headquarters that includes diplomatic missions and the crusader security companies.” The group later released the names and pictures of at least two fighters it said had been killed during the attack, one Tunisian and one Sudanese.

Libya has struggled to build a coherent and functional government to replace the one led by Colonel Qaddafi. Regional and ideological militias have battled for power and territory, often competing to coerce or intimidate the feeble institutions of the transitional government. Last summer, the rival militias broke into two warring coalitions based on opposite sides of the country.

Tripoli has come under the control of a coalition calling itself Libya Dawn, which includes moderate Islamist politicians, extremist Islamist fighters, the powerful militias from the city of Misurata and ethnic Berber groups in the West. Proclaiming its own “government of national salvation,” the Dawn coalition’s government rested its claims to legitimacy on the rump of a disbanded transitional Parliament reconvening in Tripoli.

Opponents of the Libya Dawn coalition routinely refer to it as a terrorist collaborator, happy to cooperate with extremists like Ansar al-Shariah, the Benghazi group linked to the killing of Ambassador Stevens, or the Islamic State, also known as ISIS or ISIL.

The attack on the Corinthia tested Libya Dawn’s leadership, all but forcing it to either denounce some of its militant allies or appear to countenance the violence.

Instead, the Libya Dawn’s Tripoli government tried to dodge the question, denying that the local Islamic State group was responsible and attempting, implausibly, to blame Qaddafi loyalists or the rival faction.

It was “elements of the old regime,” Omar Khadrawi, a Tripoli security official, said on Libyan television, accusing Qaddafi loyalists of “tampering with the security situation that Tripoli is blessed with.”

In a statement, the Libya Dawn government in Tripoli called the attack an attempt to assassinate its prime minister, Omar al-Hassi, who resides in the hotel but evacuated safely as the attack began.

“Fingers point at the enemies of the revolution” and the military leader of the rival faction, the statement said.

The rival coalition, under the banner Operation Dignity, is now based in the Eastern cities of Tobruk and Bayda, but it includes fighters from the Western city of Zintan, along with fighters and officers who were loyal to Colonel Qaddafi and those who renounced him. Its military effort is led by Gen. Khalifa Hifter, who tried last year to stage a military takeover, promising to rid Libya of Islamists.

Under his protection, the leaders and a slight majority of a new Parliament elected last year have moved from Tripoli to Tobruk, where they have lent formal legitimacy to his battle against Libya Dawn and its allies.

In a statement on Monday, the Tobruk-Bayda government said that the Libya Dawn forces in Tripoli “ally themselves with extremist groups who use violence to achieve political aims.” It said the attack demonstrated “the increasing levels of violence affecting civilians in Tripoli” under Libya Dawn.

In the chaos, three Libyan militant groups have pledged loyalty to the Islamic State — one based in Derna in the east, one based in the southern desert and the group based in the region around Tripoli that carried out the attack.

In December, the Tripoli group claimed responsibility for a bombing near the headquarters of the Foreign Ministry, in retaliation for a senior official’s Merry Christmas message, which the group deemed heretical. It has also claimed responsibility for the kidnapping of about 20 Egyptian Christians who were in Libya as migrant workers in the coastal city of Surt.

Suliman Ali Zway reported from Tripoli, and David D. Kirkpatrick from Cairo.

A version of this article appears in print on , on Page A4 of the New York edition with the headline: Group Linked to ISIS Says It’s Behind Assault on Libyan Hotel. Order Reprints | Today’s Paper | Subscribe