Michael Reddy scored a wonder goal three minutes into stoppage time to send Sheffield Wednesday into the next round of the LDV Vans Trophy.
The loan star looped a curling effort over Sasa Ilic from the angle of the penalty area to win the South Yorkshire derby for the Owls.
Reddy's sensational strike came just moments before an engaging contest was set to go to the extra-time silver goal.
Lloyd Owusu could have given the Owls the lead in the third minute when he raced onto a Paul McLaren through ball, but Ilic was out quickly to thwart him.
Anthony Kay flicked a header from Steven Carson's free-kick wide of Kevin Pressman's goal as Barnsley looked to assert themselves on the match.
The visitors almost profited from a goalmouth scramble in the 17th minute when Brian O'Callaghan's cross was deflected towards Pressman who dropped the ball at the feet of Kevin Betsy. Unfortunately for the 3,000 visiting fans he could not steer Barnsley in front.
Isaiah Rankin drew a reflex save from Pressman on 32 minutes, forcing the veteran keeper to beat out his low effort as Barnsley began to dominate.
Brian Barry-Murphy went close for the home side as the action swung to the other end but his delicate lob from Owusu's flick went over the bar.
Both sides went for the win with attacking formation changes at the start of the second half.
Pressman did well to clasp Betsy's effort in the 49th minute and then Barnsley new boy Jonathan Walters, signed earlier in the day on loan from Bolton, flashed a volley into the side-netting on the hour.
Walters then tested Pressman from Barnsley's next corner 12 minutes later when his header was tipped over the bar.
Adam Proudlock was denied by Ilic in the 80th minute before Reddy's late winner set up an away tie at Carlisle United.