The Bluebirds had slumped to mid-table in the Championship after picking up just one point from the last nine on offer, but the returning Kenny Miller gave them a 10th-minute lead.
The Scotland international went off injured shortly after, but his replacement Joe Mason doubled Cardiff's lead.
Daniel Drinkwater's free-kick took a wicked deflection to wrong-foot Cardiff goalkeeper David Marshall and give Barnsley a lifeline, but Aron Gunnarsson almost immediately restored the hosts' two-goal cushion before half-time.
Drinkwater struck the bar shortly after the interval as the Tykes refused to lie down, but Don Cowie and Gunnarsson's second of the match looked to have given Cardiff an emphatic win.
However, Jimmy McNulty and Ricardo Vaz Te scored for the visitors to ensure a nervy finale, but Cardiff's defence held firm.
The out-of-form hosts started strongly, with Gunnarsson, who scored in the 4-3 defeat to Peterborough in midweek, striking over from close range following Craig Conway's centre.
Their early pressure eventually told, though, as Miller marked his return to the side with a goal in the 10th minute.
The Scot had missed the Bluebirds' last two matches due to a groin injury, but he slotted home from Cowie's pass.
Miller nearly doubled his side's lead but his bullet header from Peter Whittingham's right-wing corner was magnificently tipped over by Barnsley goalkeeper Luke Steele.
The Scotland international's comeback was prematurely ended soon after, however, when he suffered a gash to the head and was replaced by Mason in the 29th minute.
If Barnsley thought that would help their cause they were mistaken as the substitute doubled Cardiff's lead five minutes later.
After Rob Edwards had fouled Mason on the edge of Barnsley's penalty area, Whittingham's free-kick was parried into the path of the 20-year-old, who tapped in.
The hosts were well on their way to ending their winless run yet they were pegged back less than two minutes later when Drinkwater's long-range free-kick took a deflection past Marshall.
It was the Tykes first sniff at goal but they barely had chance to draw breath before Gunnarsson restored Cardiff's two-goal advantage seven minutes before the interval.
Whittingham's cross-field pass found the Iceland midfielder and he fired a 20-yard drive into the bottom-right corner and past a helpless Steele.
Keith Hill made a triple substitution at the beginning of the second half, with Craig Davies, Vaz Te and Jay McEveley replacing Jim O'Brien, David Perkins and Rob Edwards in an effort to re-invigorate Barnsley.
Cardiff defender Mark Hudson was penalised for a backpass with Barnsley were given a free-kick on the six-yard line, but Drinkwater's powerful strike rattled the bar.
Instead, Cowie extended Cardiff's lead on the hour mark when he latched on to Mason's through-ball before rounding Steele.
Gunnarsson added a fifth from close range after collecting Andrew Taylor's pass in the 71st minute.
But there was still life in Barnsley and McNulty headed home from Jacob Butterfield's right-wing cross in the 82nd minute and Vaz Te narrowed the deficit even further when he fired home from Davies' lay-off four minutes later.
The fightback proved too little, too late, though, as Cardiff took the points.