The visitors took a surprise lead in the sixth minute when Jim O'Brien was played behind the Middlesbrough defence and his deflected shot looped over Jason Steele into the net.
After the break, Middlesbrough responded through Mustapha Carayol who tapped in from close range after a weak back-pass from Stephen Foster forced Luke Steele to parry the ball into his six-yard box.
Tony Mowbray's side took the lead in the 63rd minute when Rhys Williams's cross was deflected into the path of Curtis Main, who headed the ball past Luke Steele and into the net.
This joy for Middlesbrough lasted only 10 minutes as Barnsley drew level when Scott Golbourne fired in from inside the box after Rory Delap's long throw-in had caused havoc in the penalty area.
The visitors then regained their half-time lead as Jason Steele handled a back-pass, allowing Barnsley a free kick inside the area. Golbourne found Jason Scotland, who smashed the ball into the net.