Hearts needed two extra-time penalties to get past Raith Rovers and set up a fifth-round home tie against William Hill Scottish Cup holders Hibernian.
Ian Cathro's home side fell behind early on in the Tynecastle replay to Bobby Barr's deflected goal but Rory Currie and Malaury Martin both claimed their first Hearts goals, the latter from the spot.
Ryan Hardie levelled but Jamie Walker netted the second penalty and Bjorn Johnsen made it 4-2 to make sure of Cathro's second victory in his seven matches in charge.
Hearts were missing Krystian Nowak and Don Cowie following Sunday's 1-1 draw at Stark's Park and t he Ladbrokes Championship team made by far the livelier start as Jack Hamilton made saves from Kyle Benedictus and Declan McManus.
Walker saw a shot deflected over but there was little surprise when the underdogs took the lead in the 14th minute, after Barr collected the ball inside the centre circle and sped away from the Hearts midfield. Aaron Hughes closed him down but Barr's shot from 22 yards hit off the defender and looped over Hamilton.
Andraz Struna warmed up the 10,740 crowd and Kevin Cuthbert's hands with a powerful drive as the hosts finally put their visitors under some sustained pressure.
Raith repelled the danger but they somehow got caught on the counter-attack after losing possession near the halfway line in the 35th minute. Johnsen ran at the exposed defence and timed his pass to perfection to set up 18-year-old Currie to slot home on his first start.
Sam Nicholson came on at the break for his first appearance since September following knee surgery but Hearts made another flat start and Stewart came close with a turn and shot.
Rovers started to live dangerously though as the game entered the final quarter.
Nicholson headed Martin's free-kick against the bar from four yards, then Rovers escaped a strong penalty claim when Wal ker looked to have been fouled by Jean-Yves M'voto. The visitors also survived a desperate goalmouth scramble after former Hearts left-back Kevin McHattie miscued a clearance.
Both sides had late chances to avoid extra-time. Hearts left-back Lennard Sowah gifted Hardie the ball with a wayward throw-in but the Rovers substitute was too slow to shoot and his effort was blocked. Johnsen then had a header well saved before hitting the outside of the post from 18 yards.
The hosts took the lead in the 94th minute when Martin slotted home after Benedictus and Jason Thomson combined to bring down Walker.
However seven minutes later, soon after former Hearts man Rudi Skacel came on for the visitors, Hardie levelled by slotting home after a one-two with Stewart after John Souttar had miscontrolled a long ball.
Walker, who was getting treatment when the first penalty went in, scored Hearts' second spot-kick just before the interval after being tripped by Benedictus and Johnsen tapped in a rebound with six minutes left.