Barnsley deservedly took the lead five minutes before half-time through George Waring, but Gavin Massey levelled after the break before Jacob Murphy and George Moncur struck in the last 10 minutes.
Waring pounced in a crowded penalty area to touch home Conor Hourihane’s curling free-kick and give the visitors the lead.
Hourihane volleyed over just after the break, but Colchester skipper Chris Porter nearly equalised when his fierce shot was turned away by Barnsley goalkeeper Adam Davies, before Alex Wynter nodded straight at Davies from the resulting corner.
U’s goalkeeper Sam Walker pushed away Ben Pearson’s goalbound effort before Colchester equalised just after the hour-mark when Massey stabbed home Murphy’s teasing cross at the far post.
Murphy then headed home Massey’s accurate delivery with 10 minutes to go to give the U’s a precious lead.
Moncur decisively slotted home Massey’s pass with four minutes left to seal Colchester’s vital victory.