Twenty-two suspected gang members have been killed southwest of Mexico City and one soldier was wounded, the government says.
It was one of the bloodiest shootouts with security forces since President Enrique Pena Nieto took power.
According to a Defense Ministry statement, the gunfight took place on Monday (local time) in Tlatlaya on the southern fringes of the State of Mexico, which borders Guerrero and Michoacan, states plagued by gang violence.
A spokesman for federal prosecutors said all the dead - 21 men and one woman - were believed to be gang members.
The shooting started after soldiers came under fire from the suspected gang members, resulting in a gunfight lasting several minutes, the government said.
Three women, alleged kidnapping victims, were freed and one soldier was wounded but is in stable condition, the statement added.
Pena Nieto took office in December 2012 pledging to quell gang violence that has claimed more than 90,000 lives since 2007. Total homicides are down in Mexico since he took over, but the death toll has risen in parts of the country.
The State of Mexico is the country's most populous region and Pena Nieto's home state, and there murders have risen by nearly 14 per cent this year from 2013, government data show.
Michoacan has seen a jump in homicides of more than 40 per cent this year, in spite of a concerted effort by the government to pacify the state, which was under the control of the Knights Templar drug gang at the start of 2014.
In January, the Mexican government reinforced Michoacan and forged an uneasy alliance with local vigilante groups in a bid to bring the Knights Templar to heel.
However, monthly homicides in Michoacan reached their highest level since 1998 in May of this year, and the government arrested one of the vigilante leaders on Friday.