Those rescued in the raid in Helmand's Nawzad district included four children under the age of 12 and two policemen, the officials said. Twenty of the people had been arrested by the Taliban in connection with helping the government or were family members of Afghan army and police.
The reasons for the jailing of six of those rescued was still being investigated, said Abdul Qadir Bahadurzai, a deputy spokesman for the army's 215th Maiwand military corps.
The Taliban said in a statement that the people rescued were criminals accused of robbery, kidnapping, personal disputes and other crimes and were awaiting trial.
"There wasn't anyone belonging to the enemy in that prison and there wasn't enough security for it," Taliban spokesman Qari Yousuf Ahmadi said in the statement.