The Pars looked as if they had secured a place in the fifth round until goalkeeper Chris Smith committed a dreadful error in the fifth minute of stoppage time and the Irish winger tapped into an open goal.
It was the second costly goalkeeping mistake after Caley Thistle’s Jonny Tuffey failed to clear on 29 minutes, letting in Andy Barrowman.
Barrowman was brought down by defender Kenny Gillet, who was sent off for the offence, and when Martin Hardie fired in the resulting free-kick it was parried to Barrowman to nod in the opener.
Caley Thistle made just one change to the side which drew at Hibernian last time out with ever-present goalkeeper Ryan Esson – who has failed to keep a clean sheet all season – replaced by Northern Ireland international Tuffey.
Midfielder David Davis made the final appearance of his loan deal for Inverness before returning to Wolves.
Joe Cardle and Andy Kirk came in for Dunfermline, replacing Gary Mason and Paul Willis as the Pars looked to bounce back from their 3-0 loss at home to Celtic on Monday.
Hayes showed his attacking threat in front of 1,601 fans when he flashed a 30-yard free-kick over after 15 minutes.
Hardie then forced Tuffey into a save with a 25-yard free-kick which was the first shot on target.
The game sparked to life on 29 minutes when Gillet was shown a straight red card following a mix up with Tuffey.
The Inverness goalkeeper failed to clear a long ball and Barrowman nipped in before being brought down by Gillet on the edge of the box.
Referee Euan Norris wasted no time giving the Frenchman his marching orders and Hardie’s low free-kick was parried by Tuffey for Barrowman to nod home from close range.
At the opposite end Smith was called into action moments later when Billy McKay turned smartly in the box before seeing his angled shot parried wide.
Hayes then headed over for the hosts from a similar angle after connecting with a long cross from Graeme Shinnie.
Smith thwarted Hayes moments before the interval when the winger shot low from 20 yards and seconds later a Hayes cross just needed a touch from either Gregory Tade or McKay as it floated inches wide.
Smith was equal to another low Hayes shot from a 25-yard free kick but no Inverness players anticipated the rebound and the goalkeeper clawed the ball back into his grasp.
Davis’s cross-cum-shot towards the back post bounced off the crossbar as Caley Thistle continued to threaten.
Smith was almost caught out on 75 minutes when he flapped at a Davis corner but Tade’s header went wide.
Barrowman looked to punish the Highlanders further but shot just off target from outside the area.
But there was late drama when Smith miskicked a clearance after Roman Golobart had kicked the ball long in the fifth minute of stoppage time.
Hayes capitalised on the error to sweep home the equaliser from just inside the box.