Police in Venezuela have regained control of a prison east of the capital, Caracas, after a stand-off that lasted for 27 days.
More than 800 prisoners left the El Rodeo prison, after some 60 armed prisoners surrendered, officials said.
Authorities said that four inmates were shot dead by National Guard troops as they tried to escape.
The stand-off began after troops stormed the prison to put an end to violent clashes between rival groups.
More than 25 people died in the mid-June clashes and in subsequent attempts to regain full control of the prison, in the town of Guatire.
Venezuelan troops eventually took over a part of the jail known as El Rodeo 1.
But hundreds of other prisoners remained holed up in the other wing, El Rodeo 2.
The authorities said about 60 inmates there were using guns to exert control over fellow prisoners.
Nearly a month after the first riots, the Venezuelan Interior and Justice Minister, Tareck El Aissami, said some of the armed leaders had agreed to surrender.
He said the inmates had been given guarantees that their human rights would be respected.
"We've managed for all of them to come down to the yards," Mr El Aissami said. "We celebrate the triumph of peace, dialogue."
A spokesperson for prisoners' relatives, Grisel Zorrilla, told the Associated Press news agency that the inmates showed signs of dehydration but were generally in good health.
Hundreds of families remained outside the prison waiting for the latest information on the health and the likely destination of their family members.
The government said that the 831 inmates evacuated from El Rodeo were being sent to two other jails, Yare 2 and Tocuyito.
Venezuelan prisons are notoriously overcrowded and there has been a series of riots since the beginning of the year.
The Inter-American Commission on Human Rights says nearly 500 people died in prison violence last year.