A topsy-turvy encounter saw the away side take an early lead through Jim O'Brien, before Boro responded in the second half through Mustapha Carayol and Curtis Main.
Barnsley were not to be denied, however, and Scott Golbourne equalised before Scotland sealed the points with his third goal in as many games.
O'Brien saw his speculative effort from the edge of the box take a wicked deflection off George Friend and loop over Jason Steele after just six minutes.
Tony Mowbray's men drew level in the 50th minute when Ledesma's through-ball allowed Carayol to get the wrong side of Stephen Foster and, although Luke Steele managed to parry his initial effort, the rebound left the simplest of tap-ins for the Gambian.
Main then put the Teesside outfit ahead after 62 minutes as he bundled home Seb Hines' cross at the back post.
Having gone ahead, Middlesbrough were punished when Steele fumbled a high ball into the area in the 69th minute presenting Golbourne with a simple finish to bring the Tykes level.
Five minutes later the visitors were back in front as substitute Scotland rifled in off the underside of the bar.
Middlesbrough last week