Dozens of defendants faced two separate trials over the closely linked shootings, which occurred just months apart in Tunis and Sousse, but many were acquitted.
Four were sentenced to life in prison for the shooting rampage at a Sousse tourist resort in June 2015, which killed 38 people, mostly British tourists.
Five other defendants in the Sousse case were handed jail terms ranging from six months to six years, while 17 were acquitted, prosecution spokesman Sofiene Sliti said.
Three were given life sentences for the earlier attack in March 2015 at the capital's Bardo National Museum, in which two gunmen killed 21 foreign tourists and a Tunisian security guard.
Others found guilty of links to the Bardo attack were sentenced to prison terms ranging from one to 16 years, and a dozen defendants were acquitted, Sliti said.
The prosecution will appeal, he added.
One of the lawyers for relatives of French victims in the Bardo attack, Gerard Chemla, expressed "enormous bitterness" that the families had not been given more input into the proceedings.
He said a live feed of Friday's hearing had brought some degree of comfort but lamented that the relatives of those killed had not been compensated.
Chemla said he was "satisfied" that the court had handed down no death sentences, even though Tunisia retains the death penalty for terrorism offences despite carrying out no executions since the 1990s.
Attacks claimed by Islamic State armed group
The court heard that the two attacks, both claimed by the Islamic State group (IS), were closely linked.
Several defendants pointed to the fugitive Chamseddine Sandi as mastermind of both.
According to Tunisian media, Sandi was killed in a US air strike in neighbouring Libya in February 2016, although there has been no confirmation.
Among those who were facing trial were six security personnel accused of failing to provide assistance to people in danger during the Sousse attack.
That shooting was carried out by Seifeddine Rezgui, who opened fire on a beach before rampaging into a high-end hotel, where he continued to fire a kalashnikov and throw grenades until being shot dead by police.
Four French nationals, four Italians, three Japanese and two Spaniards were among those killed in the Bardo attack, before the two gunmen, armed with Kalashnikov assault rifles, were themselves shot dead.
Investigations showed one of the gunmen, Yassine Laabidi -- who was born in 1990 and was from a poor district near Tunis -- had amphetamines in his body.
His fellow attacker Jaber Khachnaoui, born in 1994 and from Tunisia's deprived Kasserine region, had travelled to Syria in December 2014 via Libya.
One suspect questioned in court, Tunis labourer Mahmoud Kechouri, said he had helped plan the Bardo attack, including preparing mobile phones for Sandi, a neighbour and longtime friend.
Kechouri, 33, said he was driven by a "duty to participate in the emergence of the caliphate," that IS supremo Abu Bakr al-Baghdadi proclaimed in June 2014 across swathes of territory the jihadists controlled in Iraq and neighbouring Syria.
Other defendants accused of helping prepare the attack said they had only discussed ideas with friends. Several alleged they were tortured in detention.
Turning the page
Survivors and relatives in France and Belgium, who watched the live feed of Friday's hearing, said it had helped them to turn the page.
"It was important for us to see, and especially to hear -- to try to understand the role" of each defendant, said one French survivor.
"Arriving at the end of the process will help us to turn the page, even if we can never forget."
The Sousse attack, which killed 30 Britons, is also the subject of proceedings in front of the Royal Courts of Justice in London, which is seeking to establish what happened.
After holding inquests into the British deaths in January and February 2017, judge Nicholas Loraine-Smith concluded that the response of Tunisian police was "at best shambolic, at worst cowardly".
There have been significant improvements in security at Tunisian tourist resorts since the massacre and, in July 2017, Britain lifted its warning against "all but essential travel" to the North African country.
The attacks and resulting travel warnings dealt a devastating blow to Tunisia's vital tourism sector from which it has taken time to recover.
Since a 2011 uprising that toppled dictator Zine El Abidine Ben Ali, jihadist attacks in Tunisia have killed dozens of members of the security forces.
Thousands of Tunisians have also travelled abroad to join jihadist organisations, according to the United Nations.