At least 19 people, including 17 foreign tourists, have been killed in an attack on a museum in Tunisia's capital, officials said.

Habib Essid, country's prime minister, said on Wednesday that men with assault rifles fired at tourists climbing from buses in front of the National Bardo Museum in central Tunis.

The attackers, who wore military-style uniforms and wielded assault rifles, were killed, the prime minister said, adding that 44 were wounded.

Newly elected President Beji Caid Essebsi in an address to the nation said that "we are in a war with terror."

"I want the people of Tunisia to understand firstly and lastly that we are in a war with terror, and these savage minority groups will not frighten us," he said.

"The fight against them will continue until they are exterminated."

Prime Minister Essid said that the attack was an unprecedented assault on the economy. "This is cowardly act to undermine our economy and a vital sector [tourism] contributing to it," he said.

"It is long battle to fight. We need all Tunisians to stand up against this act."

Polish, Japanese, Italian, Australian, French, Spanish and Colombian tourists were said to be among the victims of the attack.

Essid said that his government was working to find out the identity of the two attackers.

Earlier, an Interior Ministry spokesperson said that all hostages were freed and the security operation was over.

Television footage showed dozens of people, including elderly foreigners and one man carrying a child, running for shelter in the compound next to parliament, covered by security forces aiming rifles into the air.

The parliament building was evacuated soon after the attack.

Hundreds of Tunisians gathered in the capital, voicing their anger.

Blow to tourism

Wednesday's incident was the first on a tourist site in years in Tunisia, a shaky democracy that has struggled to keep violence at bay since the Jasmine Revolution of 2011.

The attack on such a prominent target is a blow for the small North African country that relies heavily on European tourism and has avoided major violence since the revolution.

The National Bardo Museum, built within a 15th-century palace, is the largest museum in Tunisia with collections covering two floors, and it houses one of the world's largest collections of Roman mosaics.

It was not immediately clear who the attackers were.

Tunisia has been more stable than other countries in the region, but it has struggled with violence by armed groups in recent years, including some linked to the Islamic State of Iraq and the Levant (ISIL) group.

Fighters linked to al-Qaeda's North Africa arm have also occasionally targeted Tunisian security forces.

Wednesday's assault was the worst attack involving foreigners in Tunisia since an al-Qaeda suicide bombing on a synagogue killed 21 people in the tourist island of Djerba in 2002.