Depends, if they were paroled and exchanged, no, as they were returning to their Confederate Army commands.
If they took the Oath of Allegiance to the United States before war's end, they could be released, usually to remain behind Federal lines
till cessation of hostilities.
After the war, they were eventually released after subscibing to the Oath.