You are not permitted to download, save or email this image. Visit image gallery to purchase the image.
Louisa Vesterager Jespersen (24), from Denmark, and Maren Ueland (28), from Norway, were killed in their tent in December near the Moroccan village of Imlil, a popular hiking destination.
The anti-terrorism court in Sale, near Rabat, had heard at earlier sessions that the men had pledged allegiance to Islamic State in a video days before the murder.
Two of them - Abdessamad Ejjoud and Youness Ouziyad - had told the court they had decapitated the women, while the third - Rachid Afatti - said he filmed the killing and posted the footage online.
A fourth man who said he had left the group before the murders got a life sentence.
On Thursday (local time), the men said: "May God forgive us" and one added: "There is no God but Allah" when the judge asked them for their final statements before giving his verdict.
Their lawyer, Hafida El Maksaoui, said she would appeal against the sentences.
Twenty other people who prosecutors said had links to the killers were sentenced to between five and 30 years for forming a criminal gang to commit terrorist acts, encouraging terrorism, undermining public order and other crimes.
All were Moroccan apart from one Swiss national, Kevin Ziller Guervos, who was also accused of persuading people to join militant groups and got a 20-year sentence.
The court ordered the four main convicts to pay 2 million dirham ($US209,000) compensation to Ueland's family.
Lawyers for Jespersen's family had asked for the state, not the murders to pay, saying the men would not be able to afford it. But the court turned down that request and made no other order for payments.