About 20 of the dead were reportedly members of the drug gang Los Zetas who were said to have recently demanded to be released from prison.
Fighting erupted again hours later with eight more prisoners being stabbed to death by other inmates, Angeles Moreno, the Public Safety Department spokesman, said.
Moreno said that police were investigating the cause of the incident.
Los Zetas have been fighting the powerful Sinaloa cartel for influence over drug trafficking and other criminal activity.
Sinaloa state, on the west coast of Mexico, is a primary location for drug trafficking from South America to the US, and is a region for production of drug crops and synthetic narcotics.
Last week, Jesus Aguilar, the governor of Sinaloa, said that the 6,000 prisoners in the Mazatlan jail was too many.
Drugs gangs regularly attempt to break their members out of jail or bribe authorities to release them.
Separately, drugs hitmen killed at least 13 police officers in two different incidents.
The strikes constituted one of the most deadly day for security forces since Filipe Calderon, the president, stepped up a military crackdown on drugs cartels in late 2006.
In the first attack, 10 officers were killed when gunmen opened fire on a police convoy near a high school as it was returning from a patrol in the city of Zitacuaro, in Michoacan.
At least 13 other officers were wounded and taken to hospitals in Mexico City, the nearby city of Toluca and to Morelia, the Michoacan state capital, for treatment.
In a statement, the federal Public Safety Department said several of the attackers were also killed or wounded, but did not provide a number or identify the assailants.
Later three more officers were killed in a shootout with gunmen in the drug-plagued northern border city of Ciudad Juarez.
|Security forces have been unable to
stem drug violence nationwide [EPA]
The latest deaths in litany of assassinations and violent crime came as Felipe Calderon, the Mexican president, defended his crackdown on drug gangs.
Calderon vowed no let up in the campaign despite criticism that violence has only surged since he deployed thousands of troops and federal police in late 2006 seeking to crush the drug gangs.
"I'm clearly convinced that we would be in a much worse situation if we hadn't decided to fight criminals," he wrote in the 5,000-word essay, which was published by several newspapers.
"If we remain with our arms crossed, we will remain in the hands of organised crime, we will always live in fear."
Calderon also blamed Mexico's violence on the high demand for illegal drugs from across the border in the United States.
Michoacan is known as a stronghold of the La Familia cartel, known for beheading its rivals and making bold attacks on government security forces.
At least 23,000 people, mainly traffickers and police, have been killed in drug-related violence since Calderon launched an army-led offensive against drug gangs after taking office in December 2006.