Men with assault rifles fired at tourists climbing from buses in front of the National Bardo Museum in central Tunis near the country's parliament, sending dozens sprinting for safety. Two gunmen were killed, but Prime Minister Habib Essid said a manhunt was on for at two or three others.
The identity of the attackers wasn't clear. Twitter accounts associated with the extremist Islamic State group based in Syria and Iraq were described as overjoyed at the attack, urging Tunisians to "follow their brothers," according to Rita Katz of SITE, a U.S.-based organization that monitors militant groups.
About 50 people were wounded in the attack, which began after noon local time, according to Tunisian state television.
Security forces immediately flooded the area around the museum, and Tunisia's parliament building, where deputies were debating a new anti-terrorism law, was evacuated.
Dozens of tourists scrambled from the museum holding hands or linking arms as Tunisian security forces pointed their guns toward an adjacent building. At least one couple carried two children.
According to Essid, the dead include two gunmen, a Tunisian security officer and a Tunisian cleaning woman, while the rest were tourists from Italy, Poland, Germany and Spain. The Spanish Foreign Ministry has confirmed one dead.
Tunisia has been struggled to keep extremist violence at bay since the overthrow of its dictator in 2011, and the attack was the worst on a tourist site since an al-Qaida car bomb killed 21 people — mostly Germans — in 2002.
"Our nation is in danger," Essid warned in an address on national television Wednesday evening after the siege ended.
"We will be merciless in the defense of our country," he added, describing the attack as an unprecedented assault on Tunisia's economy. He promised increased security in tourist zones and asked residents to be extra alert.
Several other people were reported wounded in the attack, including three Poles and at least two Italians. The Italian Foreign Ministry said 100 other Italians had been taken to a secure location.
The United States, France, the United Arab Emirates and the United Nations denounced the bloodshed. U.S. Secretary of State John Kerry said Washington "condemns in the strongest possible terms today's deadly terrorist attack" and praised Tunisia's "rapid response" to resolve the hostage situation and restore calm.
The attack was a strong blow to Tunisia's efforts to revive its crucial tourism industry.
Some of the Italians at the museum were believed to have been passengers aboard the Costa Fascinosa, a cruise liner that had docked in Tunis while on a seven-day tour of the western Mediterranean. Ship owner Costa Crociere confirmed that some of its 3,161 passengers were visiting the capital and that a Bardo tour was on the itinerary, but said it couldn't confirm how many passengers were in the museum at the time.
The Bardo, built in a 15th century palace, is the largest museum in Tunisia and houses one of the world's largest collections of Roman mosaics among its 8,000 works. The museum, 4 kilometers (2 ½ miles) from the city center, has a new wing with contemporary architecture that was built as part of a 2009 renovation.
During the Roman Empire, Tunisia was called the province of Africa and was home to several large cities which are now popular ruins, including the great amphitheater of El Djem, the ruins of Sbeitla and in the north, Dougga, known by UNESCO as the "best preserved small town in North Africa."
"It is not by chance that today's terrorism affects a country that represents hope for the Arab world. The hope for peace, the hope for stability, the hope for democracy. This hope must live," French Foreign Minister Laurent Fabius said in a statement minutes after the crisis ended.
Speaking at the Louvre museum to call for international efforts to preserve the heritage of Iraq and Syria against extremist destruction, French President Francois Hollande said he had called Tunisia's president to offer support and solidarity.
"Each time a terrorist crime is committed, we are all concerned," Hollande said.
Tunisia recently completed a rocky road to democracy after overthrowing its authoritarian president in 2011, seen by many as the start of the so-called Arab Spring. The country has been more stable than others in the region, but has struggled with violence by Islamic extremists who have sworn allegiance to both al-Qaida and the Islamic State group.
A disproportionately large number of Tunisian recruits — some 3,000, according to government estimates — have joined Islamic State fighters in Syria and Iraq.
The U.S. Embassy in Tunis was attacked in September 2012, seriously damaging the embassy grounds and an adjoining American school. Four of the assailants were killed.
Overall, though, violence in Tunisia in recent years has been largely focused on security forces, not foreigners or tourist sites.
In October 2013, a young man blew himself up on a beach in the coastal town of Sousse after being chased from a hotel, causing many to expect a new wave of attacks on tourism. None materialized until now.
North Africa analyst Geoff Porter said an attack on a tourism site has long been expected as the militants come under pressure from increasingly effective Tunisian security forces.
"Today's attack did not come out of nowhere. In fact, it comes amid ongoing counterterrorism efforts elsewhere in the country," he said about the attack. "Increasing pressure on terrorist activities ... may have squeezed the balloon, with terrorists seeking softer targets with more symbolic impact in the capital."
The attack came the day after Tunisian security officials confirmed the death in neighboring Libya of Ahmed Rouissi, leading suspect in Tunisian terror attacks and in the killings of two opposition figures in Tunisia.
Rouissi had become a field commander for the Islamic State in Libya and died fighting near the town of Sirte, highlighting how Libya has increasingly become a sanctuary for Tunisian radicals.
Tunisia has repeatedly expressed concern over the security threat from Libya, where central government has broken down since the 2011 ouster of Moammar Gadhafi and is now run by competing militias.
Schemm reported from Rabat, Morocco. Elaine Ganley and Jamey Keaten in Paris, Nicole Winfield in Rome, and Monika Scislowska in Warsaw, Poland, contributed to this report.
Copyright 2015 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.