Police secure an area near the Ghriba synagogue following a shootout on the resort island of Djerba on May 9, 2023. (AFP)
Four other visitors to the Ghriba synagogue on Djerba island and five more security officers were wounded in the attack, according to the ministry.
In a statement, the Tunisian foreign ministry identified the two worshippers killed as a Tunisian, aged 30, and a French national, aged 42, but did not give their names.
The attack took place after the assailant first shot dead a colleague and took his ammunition, according to the interior ministry.
He then went to the Ghriba synagogue -- Africa's oldest -- as hundreds of people were taking part in the annual pilgrimage, which was drawing to a close on Tuesday night.
The synagogue was previously targeted in a suicide truck bombing that killed 21 in 2002.
"Investigations are continuing in order to shed light on the motives for this cowardly aggression," the interior ministry said, refraining from referring to the shooting as a terrorist attack.
US State Department spokesman Matthew Miller condemned the shooting.
"The United States deplores the attack in Tunisia coinciding with the annual Jewish pilgrimage that draws faithful to the El Ghriba Synagogue from around the world," he said on Twitter.
"We express condolences to the Tunisian people and commend the rapid action of Tunisian security forces."
The French embassy in Tunisia announced it had set up a "crisis unit" and an emergency hotline following the attack.
The sound of gunshots at the synagogue had sparked panic among the hundreds of pilgrims, local media reported.
According to organisers, more than 5,000 Jewish faithful, mostly from overseas, participated in this year's pilgrimage to Ghriba, which resumed in 2022 after two years of pandemic-related suspension.
Tuesday's shooting came as the tourism industry in Tunisia was finally enjoying a rebound from pandemic-era lows, as well as from the aftereffects of a pair of attacks in Tunis and Sousse in 2015 that killed dozens of foreign visitors.
The Ghriba attack also comes as Tunisia goes through a severe financial crisis that has worsened since President Kais Saied seized power in July 2021 and rammed through a constitution that gave his office unlimited powers and neutered parliament.
Short link: