President Ollanta Humala told the Peruvian radio station RPP that the guerrillas freed the captives as troops and police were closing in. He said no negotiations with the rebels had taken place.
"Seeing themselves surrounded, they released the 36 hostages," Humala said from the Summit of the Americas in Cartagena, Colombia. He said the operations by security forces will continue until they track down the kidnappers.
The government had said about 1,500 soldiers and police were involved in search operations.
Peruvian television showed images of the freed men waving to the camera and then praying. The men appeared tired as they climbed aboard army helicopters.
Freed hostage Rigoberto Muniz told TV channel N that the workers had been in the wilderness and were fed once a day, usually noodle soup. He and others said the rebels hadn't mistreated them.
Ronald Pacheco, another of the workers, told RPP that the rebels "freed us at 4 in the morning, told us we could go and pointed out the way back." He said the rebels hadn't made clear why they abducted them.
Local officials said previously that the rebels were demanding a $10 million ransom.
Humala said government officials had explained to the workers' employers that "the government's policy is not to negotiate with kidnappers, and we won't permit a company to stray from that policy."
"This has been an impeccable operation. Nothing has been ceded to these criminal terrorists," the president said.
Three police officers and two soldiers were killed during the search, a military official said on condition of anonymity. He declined to be identified because he wasn't authorized to speak publicly.
The government had previously confirmed that one police officer was killed Thursday when a helicopter involved in the search was attacked with gunfire.
Humala didn't give details about casualties.
The Peruvian workers were kidnapped about 3 a.m. Monday from hotels in the hamlet of Kepashiato, near the Camisea natural gas field. Mayor Rosalio Sanchez said the rebels lingered for a few hours, summoning about 20 residents to an assembly where they condemned the government and the gas industry.
Those who were abducted were working as contractors on gas industry projects. Officials previously said there were 40 workers involved, but on Saturday officials confirmed that there were 36 and that all had been freed.
The Swedish construction company Skanska has said 29 of the kidnapped workers were its employees. Others work for the Peruvian company Construcciones Modulares.
Once freed, the men walked out of the jungle to the tiny town of Chuanquiri, said Susano Guillen, the local governor.
"They arrived at 11 in the morning, and we were surprised to see them arrive," Guillen said by phone. "We gave them water because they were hungry and thirsty. Then they left for the town of Kiteni." Guillen said many of their relatives were there.
Guillen said the 36 men arrived wearing orange work clothes and had hiked about seven hours to reach the town.
Johan Karlstrom, CEO and president of Skanska, said the workers were in good health.
"It is hard to express the relief and comfort I feel over the fact that our colleagues now can reunite with their families and return home," Karlstrom said in a statement.
He thanked Peruvian authorities and the company's client, consortium Transportadora de Gas del Peru, for their cooperation.
Such mass abductions are rare in Peru.
The Shining Path, which is financed by the cocaine trade, is a small remnant of the Maoist rebel group that terrorized Peru in the 1980s and 1990s. It is believed to number about 300-500 fighters centered in the Ene and Apurimac Valley region where more than half of Peru's coca is grown. The town where the kidnapping occurred is in an adjacent region.