CAIRO — Islamic State suicide bombers targeted the offices of Libya’s electoral commission in Tripoli on Wednesday, killing at least 12 people in one of the most brazen assaults on the Libyan capital in years.

The attackers, strapped with explosive belts, entered the building and opened fire on employees at the High National Election Commission (HNEC), engaging in a gun battle with guards before detonating the explosives.

Plumes of black smoke rose from the office building in the Ghout al-Shaal enclave near Tripoli’s downtown, according to images and video posted on social media by witnesses.

“This breach targeted democracy, not just the HNEC,” the electoral commission’s director, Emad al-Sayah, told a news conference. “The choice and future of Libyans were targeted.”

A few hours after the attack, the Islamic State asserted responsibility in a statement posted on Amaq, the militant group’s news agency. The group said it had dispatched two fighters, Abu Ayoub and Abu Tawfeeq, to “target the apostate ballot stations,” suggesting that the goal of the attack was to harm Libya’s ability to hold elections this year, which U.N. and Western officials view as essential to bringing stability to the war-riven nation.

The bombing was also a reminder of the lingering dangers posed by the Islamic State more than a year after U.S. airstrikes and Libyan militias pushed the militants from their stronghold of Sirte, 230 miles east of Tripoli. In recent months, the militants have targeted security checkpoints and government buildings in other towns and cities.

Wednesday’s attack was their deadliest attack since they were expelled from Sirte. U.S. and other Western intelligence and security officials think the Islamic State is seeking to regroup in the south as well as gain ground in nearby countries such as Tunisia, Mali and Niger. The militants are also widely believed to have sleeper cells in Tripoli and other areas.

In its statement, the Islamic State said the two suicide bombers detonated their explosive vests “after running out of ammunition.” The group also claimed that 15 people died in the assault. Libyan officials placed the death toll at 12, including at least two of the commission’s guards, who fought the attackers.

Sayah told reporters that the database of the commission, which has registered nearly 1 million new voters across Libya, had not been damaged. No date has been set for the elections. Many Western officials and analysts remain skeptical that Libya, which has two competing governments, in the east and west, can become secure enough to hold a vote.

Libya has been gripped by violence since dictator Moammar Gaddafi was toppled in the populist Arab Spring revolts in 2011. Rival militias have sought to gain power, influence and control over Libya’s oil wealth and have carved up regions and cities, including the capital, into zones of influence.

The bombing was the latest turmoil to strike the country in recent weeks. For the past month, Libyans were consumed by the hospitalization of Khalifa Hifter, a powerful military commander whose forces control the eastern part of Libya. Rumors of his death spread like brushfire and triggered a potential succession struggle. But the 75-year-old returned to the country last week from France in apparent good health.