Tunisia suffered its worst terrorist attack in recent memory Friday as a gunman fatally shot 39 people at a beach resort, an assault that rattled the fragile democracy and threatened to devastate its critical tourist industry.

The attack in the Tunisian resort city of Sousse, about 90 miles south of the capital, Tunis, has raised fears that Tunisia has become a primary target for Islamist extremists seeking to destabilize the region.

The Islamic State asserted responsibility for the attack. In a statement circulated by Islamic State supporters online, the group said the attack had been carried out by a man named Abu Yahya al-Qayrawani. Tunisian officials earlier Friday said the gunman, who was shot and killed by police, was from the Tunisian city of Kairouan, in a province known as a stronghold of ultraconservative Islamists.

The attacker opened fire as tourists lounged in the sun, according to witnesses. The gunfire sent beachgoers into a panic, witnesses said. The gunman killed 39 people and wounded at least 35. The victims hailed from at least six countries, according to authorities.

Friday’s carnage was one of a spate of terrorist attacks in separate countries Friday and comes just three months after a pair of young Tunisian gunmen with apparent links to the Islamic State killed 22 people at the prominent Bardo museum in Tunis. Most of those victims were also foreigners.

The attacks in Sousse and Tunis both appeared to be aimed at crippling Tunisia’s tourism industry, which accounts for about 15 percent of the country’s gross domestic product.

Tunisia has grappled with the rise of Islamist extremism in the wake of the Arab Spring, which ousted the country’s autocratic but secular president in 2011. Westerners have long flocked to Tunisia’s sunny Mediterranean beaches, and the tiny North African nation has been lauded as the sole success story of the Arab revolts. But Tunisia has also contributed thousands of foreign fighters to Islamic State ranks in Syria and Iraq, analysts say.

Many Tunisians are also thought to have joined extremists in neighboring Libya, where the collapse of the state has given rise to an array of armed groups. The perpetrators of the attack on the Bardo museum in March were two Tunisian youths who were apparently radicalized and later trained by Islamist extremists in Libya. Tunisian lawmakers have not yet passed a draft terrorism law that the government says will enable security forces to pursue militants more easily.

Tunisians, Germans and Belgians, and at least one Irish citizen, were among the dead in Friday’s attack, the BBC reported. British Foreign Secretary Philip Hammond said that at least five Britons were also among those killed and that that number is expected to rise.

A tourist from Dublin, Elizabeth O’Brien, told Ireland’s RTE Radio that it was about noon Friday when she heard gunshots, which she initially mistook for fireworks. Then, when she realized something was wrong, she raced to the shore to grab her two sons.

“I just ran to the sea to my children and grabbed our things, and as I was running towards the hotel, the waiters and security on the beach started saying, ‘Run, run, run!’ ”

Glenn Leathley, a Briton whose daughter Olivia was in Sousse, told the BBC that his daughter called him, crying and in a panic, telling him that there was gunfire on the beach but that she was with her boyfriend in their room at the RIU Imperial Marhaba hotel.

“About five minutes later, she rang me again in a panic and said, ‘They’ve come into the lobby,’ ” he said, adding that “she seemed to know that they were there and at that point she started running.” He told the BBC that his daughter had since made contact to tell him she was safe.

Sarah Wilson and her fiance, Matthew James, from Pontypridd, Wales, were lounging on sun beds when the gunman opened fire, Wilson told Britain’s Mirror newspaper. James threw himself in front of the gunman and was shot in the shoulder, chest and hip.

“He was covered in blood from the shots, but he just told me to run away. He told me: ‘I love you, babe. But just go — tell our children that their daddy loves them,’ ” Wilson said. She told the paper that her partner had survived.

The Ennahda party, part of Tunisia’s ruling coalition government, released a statement condemning the attacks.

“We offer our prayers to the victims and their families, and call upon authorities to swiftly find the perpetrators and bring them to justice.

“Tunisia has undergone a remarkable democratic transition and is the success story of the Arab Spring,” the statement continued. “But our country is still fragile. There is a tiny but poisonous fringe of society across our region which has wrongly interpreted the Islamic faith and wishes to destroy Tunisia’s progress at any cost.”

Cunningham reported from Baghdad. Karla Adam in London, Brian Murphy in Washington, Hugh Naylor in Beirut and Mustafa Salim in Baghdad contributed to this report.

Read more:

Ramadan had brought fears of new Islamic State attacks

France launches terrorism investigation into attack at gas factory

VIDEO: Decapitated head found in suspected French Islamist attack

Today’s coverage from Post correspondents around the world