The judges said the basic rights of the jailed men had been violated, but made it clear they were not ruling on the men's guilt or innocence.
The 1997 attack took place in southern Chiapas state, which was deeply divided at the time over a Zapatista uprising.
The court's ruling was met with dismay by relatives of the victims.
They also warned of the risk of renewed violence if the freed men returned to their communities.
The brutal attack took place three days before Christmas in 1997 in the remote mountain village of Acteal.
Over several hours, the attackers used machetes and automatic weapons to kill 45 people, including pregnant women and children, many of whom were attending a prayer meeting.
The victims were seen as supporters of the Zapatista rebels, who were fighting for greater rights for the area's indigenous population at the time.
Most of those arrested were Indians from a nearby town and of the same Tzotzil community as the victims. They were accused of being the government-linked paramilitaries blamed for the attack.
In its judgement, the Supreme Court cited a number of irregularities in the investigation and prosecution of the suspects, including the lack of proper legal representation and translators who spoke the Tzotzil language.
"During the investigation, their constitutional rights were violated," the court said in a statement. "The majority of cases... were based on the use of illegally obtained evidence."
As well as ordering the immediate release of 20 prisoners, the court has also ordered the retrial of six more.
The ruling was condemned by families of the victims as well as some human rights groups, who say that despite the botched investigations the right people were jailed.
"We feel a lot of pain in our hearts because we think it's unfair that after almost 12 years these people are being freed when we know they are responsible because we saw them," said Antonio Arias, who was wounded in the attack.
Arturo Farela, who has argued the innocence of those tried, warned of possible tensions if the men return to their home town of Chenalho.
"The social fabric of Chenalho is weak and the government needs to investigate and punish the people who are truly responsible but also those who ordered the killings," he told the Associated Press.