Hibs started the better team but Arnaud Djoum and Sam Nicholson put Hearts in control with well-taken strikes late in the first half.
The visitors kept plugging away and a brilliant Jason Cummings header gave them hope with 10 minutes left, and they stunned their hosts when Hanlon scrambled home following a corner.
Before the game Hearts had paraded the manager who led them to their 5-1 final in over their Edinburgh rivals in 2012, Paulo Sergio, but it was the away fans who were celebrating after the game.
Sergio's reception dwarfed the one afforded to China-bound Osman Sow, whose place in the team was taken by new signing Abiola Dauda.
Hibs began brightly and Neil Alexander was at full stretch when he stopped John McGinn's curling effort finding the same corner of the net where he dispatched his League Cup semi-final winner the previous weekend.
Cummings had the ball in the net after a fifth-minute corner but he was just offside when he latched on to Darren McGregor's shot and the flag went up.
Hearts were forced into a change inside 10 minutes when Prince Buaben pulled up, handing Don Cowie his debut, and Anthony Stokes headed inches over under pressure following Liam Henderson's corner as the visitors continued to press.
Hibs remained comfortable but fell behind in the 32nd minute, just after Kevin Thomson replaced the injured Dylan McGeouch.
Lewis Stevenson dived to head away Callum Paterson's cross and, as the Hearts right-back claimed for a penalty for handball, Djoum picked up the loose ball and smacked a shot that swerved away from Mark Oxley and bounced in off the far post.
Hearts defenders Alim Ozturk and Jordan McGhee were booked as the hosts prevented Hibs from breaking and Robbie Neilson's team moved further ahead with a minute of the half remaining.
Nicholson brought down Paterson's crossfield ball on his chest before setting himself to drill a shot low inside Oxley's near post from 16 yards.
The midfielder was booked for gesturing towards the away support, who responded by throwing several paper cups.
Both sides lost their captains to injury early in the second half, Ozturk failing to shake off a knee problem and David Gray succumbing to a knock.
The physical battle continued with Hibs midfielder Marvin Bartley and Hearts substitute Juwon Oshaniwa booked for fouls along with McGregor, whose high, studs-up attempted block on McGhee sparked a mass confrontation that led to both assistant referees racing on and ended with Blazej Augustyn and Stokes getting yellow cards.
Hibs were pushing Hearts back and creating half-chances. Stokes was off target with two free-kicks and also shot into the side net and Henderson lacked conviction as his shot was blocked.
Hibs were rewarded for their persistence when Cummings got his head to Henderson's cross and looped a header which fell slowly down underneath the crossbar.
Stevenson had a quick chance to level when he turned in the box but shot straight at Alexander and Hibs survived a scare when Dauda glanced Paterson's delivery just wide.
The leveller soon came when Hanlon swung a leg out on the ground to force the ball home after Alexander had stopped McGregor's header.
And Hibs held on for a replay after substitute Niklas Gunnarsson lobbed a clearance off his own bar before Thomson cleared Augustyn's header off the line.