The public prosecutor, the victim and regional authorities appealed to the Supreme Court, asking for a rape conviction and longer sentences. The men's lawyers wanted them acquitted, arguing that the woman consented.
Women's rights advocates had expressed anger about what they saw as the lower court's leniency and the confirmation of that sentence by a second court. They have held national protests since the 2017 verdict.
Amnesty International welcomed the ruling. "We are delighted that justice has finally prevailed in this horrific case and that the rights of the survivor have been upheld," said Massimo Moratti, the human rights group's deputy director of the European regional office. "But the journey to get here has been long, and caused the woman involved further unnecessary and avoidable suffering.
"This case shows exactly why it's so crucial for Spain's Penal Code to be amended," he said in a statement. The judges heard both sides' arguments in a televised session Friday and gave their verdict after just two hours.
They said that, in accordance with legal precedent, the attack could be classified as rape, which was aggravated by being performed in a group. The victim didn't consent to the acts, the judges said, and was subjected to 10 acts of sexual aggression in "a genuinely intimidating scene."
They found that the men also made videos of their attack. Prosecutors said the men boasted about the 2016 attack during Pamplona's San Fermin festival on a WhatsApp group named "La Manada," or "The Animal Pack."
The case brought widespread criticism from women's groups after the first court agreed to study a defense detective's report on the woman's behavior after the incident, which some said made it appear the victim was on trial.
Viviana Waisman, president of the international nonprofit organization Women's Link Worldwide, which pursues social change through the law, said that courts too often doubt the victims and fail to protect them.
She said Friday's decision "sends a clear message that judges must apply the law ... and when victims of sexual violence come forth they must be believed, not questioned."