Peru's Shining Path early on Saturday released 36 gas pipeline workers it took hostage six days ago in a remote jungle region in southern Peru, the government said, saying security forces had clashed with the rebels.
The Defence Ministry said the rebels let the workers go before dawn after they were circled by 1,500 security agents and tried to flee, in a victory for President Ollanta Humala, who fought the group while in the army in the 1990s.
Officials said the hostages were safe and in good health.
Prime Minister Oscar Valdes told state news agency Andina that at least one security agent died during the rescue operation, while Canal N cable television station said three police and two rebels died in the clashes.
Some of the captives said they walked through the jungle for hours after they were released. One said the hostages had not been mistreated but that the experience was a nightmare.
The rebels are holdouts from the Shining Path insurgency that entered Peru's thriving cocaine trade after their Maoist founders were imprisoned in the 1990s.
These kidnapping terrorists released the 36 hostages after they found themselves surrounded, Humala told RPP radio from Colombia, where he is attending the Summit of the Americas.
The operations have come at a cost, but for the moment I'm not going to say what the cost was, added Humala. In some areas, there has been a confrontation and in others a chase.
Humala said he would visit the area of the kidnapping on his return from the summit and that the government had not negotiated with the rebels, who had demanded a $10 million (6.3 million pounds) ransom and explosives.
We have not given in to any blackmail by these terrorist organizations, Humala told reporters in Cartagena, Colombia.
They (the freed hostages) are having medical checkups and, of course, we want to know what happened to them, he added, saying security forces would chase the kidnappers all over Peru until they were captured.
Those freed said the rebels had simply let them go.
They (the rebels) decided to free us early in the morning. We have been walking since four o'clock in the morning, one of the hostages who identified himself as Fortunato told local television, which showed footage of some of the freed hostages in their orange work overalls.
Swedish company Skanska, which services a pipeline that carries gas from Peru's Camisea gas fields, said this week that more than two dozen of its employees were detained. Peruvian company Ransa said nine of its workers were missing.
The Shining Path rebels, who are now too weak to threaten the government, had not carried out a large-scale kidnapping since 2003, when they captured 70 workers employed by Argentine company Techint building the Camisea pipeline.
In February, the government caught Shining Path leader Florindo Eleuterio Flores, the last high-ranking figure from the group's historic core, who goes by the nom de guerre Artemio.
The Shining Path launched a war to overthrow the state in 1980, and some 70,000 people were killed in the conflict.
(With reporting by Terry Wade in Lima and Mario Naranjo in Catagena; Writing by Simon Gardner; Editing by Peter Cooney)