Aberdeen missed the chance to go top of the Scottish Premier League after losing to Inverness at Pittodrie.
The visitors took the lead when Billy McKay met Andrew Shinnie's cross.
Josh Magennis slotted the ball into the net just before the break to put the home side level and the Northern Irishman chipped a neat second.
But Gary Warren scored a second-half equaliser before a late winner from McKay allowed Inverness to leapfrog the Dons into second place in the table.
Terry Butcher's side move level on points with Scottish Premier League leaders Celtic, who have a superior goal difference and two games in hand.
The home side started brightly and Magennis burst through on the right-hand side and almost sent a low drive through Antonio Roguero's legs, but the keeper managed to recover.
The game descended into a lull as the first-half wore on, but the home fans were rudely awakened when Inverness went ahead through a controversial goal.
Aberdeen fans claimed for a free-kick as Richie Foran challenged Jamie Masson in the midfield area but referee Alan Muir waved play on.
Shinnie crossed from the left and McKay got the slightest of touches to take the ball past Jamie Langfield and into the net.
The home supporters were left incensed again when Rory Fallon went down in the Inverness box as he and Josh Meekings both went up for an aerial ball, Muir again letting play continue.
However, a minute before half-time, their boos turned to cheers when Johnny Hayes cut the ball across the face of goal and Magennis coolly side-footed the ball home.
The referee was given a rapturous welcome for the second half, mainly because he was a different referee. Muir had suffered a calf injury, and was replaced by Craig Charleston - much to the delight of the Aberdeen supporters.
The Dons carved out a couple of early chances as they went looking for a second goal, with Reguero tipping a Joe Shaughnessy header over and Hayes going close with a low, left-footed drive.
And the pressure told when Magennis spun past Meekings and flicked the ball over Reguero to put the home side ahead.
However, Aberdeen then seemed to ease off and Warren connected with Aaron Doran's inswinging corner to bring the visitors level.
Hayes, playing against his former club, was stung into action and took the ball forward from his own right-back area and blasted a shot from 30 yards which went just wide of Reguero's right-hand post.
As the game became stretched in the latter stages, Langfield had to look smart to prevent a Scott Vernon own goal and then deflect a Shinnie shot wide.
And then Shinnie tried to set up Richie Foran when one-on-one with the keeper, but overhit his pass when he should perhaps have taken the chance himself.
Then, with the game seemingly drifting to a draw, Russell Anderson misjudged a long, lofted ball and chested it into the path of McKay, who steered the ball into the back of the net for the winner.
Sorry, we were unable to load match stats, please try again.
Live Text Commentary
Full Time The referee blows for the end of the match.90:00 +0:40
The referee blows for offside. Jamie Langfield restarts play with the free kick.89:47
Gary Warren fouled by Cameron Smith, the ref awards a free kick. Direct free kick taken by Josh Meekings.86:37
Gary Warren takes the direct free kick.86:37 Substitution
Substitution Johnny Hayes goes off and Declan McManus comes on.86:37
Rory Fallon gives away a free kick for an unfair challenge on Owain Tudur Jones.85:44
Centre by Johnny Hayes, save made by Antonio Reguero.85:24
Corner from the right by-line taken by Johnny Hayes, Josh Meekings makes a clearance.84:58
Inswinging corner taken by Johnny Hayes from the left by-line, clearance made by Gary Warren.84:48
The ball is swung over by Johnny Hayes, clearance by David Raven.83:50
The assistant referee flags for offside against Billy McKay. Mark Reynolds restarts play with the free kick.82:35
Billy McKay fouled by Andrew Considine, the ref awards a free kick. Gary Warren takes the free kick.81:39 Goal scored
Goal! - Billy McKay - Aberdeen 2 - 3 Inverness A goal is scored by Billy McKay from inside the box to the bottom left corner of the goal. Aberdeen 2-3 Inverness CT.79:13 Substitution
Substitution Cameron Smith comes on in place of Jamie Masson.72:47
Free kick taken by Graeme Shinnie.72:47 Booking
Booking Scott Vernon goes into the referee's book.72:39
Free kick awarded for an unfair challenge on Owain Tudur Jones by Scott Vernon.72:06
Andrew Shinnie has an effort at goal from just outside the box which goes wide of the right-hand upright.71:44
Foul by Mark Reynolds on Billy McKay, free kick awarded. Gary Warren restarts play with the free kick.71:20
The referee blows for offside against Billy McKay. Indirect free kick taken by Jamie Langfield.
Free kick awarded for a foul by Johnny Hayes on David Raven. Graeme Shinnie restarts play with the free kick.70:16
Corner taken short by Johnny Hayes.69:02
Effort on goal by Andrew Shinnie from deep inside the penalty area misses to the right of the target.66:31
Owain Tudur Jones challenges Niall McGinn unfairly and gives away a free kick. Direct free kick taken by Mark Reynolds.65:43
Josh Magennis takes a shot. Save made by Antonio Reguero.64:32
Effort from the edge of the box by Richie Foran goes wide of the right-hand upright.64:15
Aaron Doran sends in a cross, save by Jamie Langfield.63:24
Shot from just outside the area by Rory Fallon goes over the bar.61:47
Johnny Hayes has an effort at goal from outside the penalty box missing to the wide right of the target.61:20
Corner taken right-footed by Aaron Doran, clearance made by Russell Anderson.61:11
Centre by Aaron Doran, Mark Reynolds manages to make a clearance.59:14
Jamie Langfield takes the indirect free kick.59:14 Substitution
Substitution Ross Draper is brought on as a substitute for Philip Roberts.59:14
The offside flag is raised against Andrew Shinnie.58:23
The ball is delivered by Niall McGinn.57:38
Assist by Aaron Doran.57:38 Goal scored
Goal! - Gary Warren - Aberdeen 2 - 2 Inverness Gary Warren scores a headed goal from close range. Aberdeen 2-2 Inverness CT.57:15
Corner taken by Aaron Doran from the left by-line,56:14
The ball is swung over by Niall McGinn, save by Antonio Reguero.55:14
Andrew Considine concedes a free kick for a foul on Aaron Doran. David Raven restarts play with the free kick.54:14
Outswinging corner taken left-footed by Johnny Hayes from the left by-line, clearance made by Owain Tudur Jones.53:37
Free kick awarded for a foul by Gary Warren on Josh Magennis. Free kick crossed left-footed by Johnny Hayes, clearance made by Billy McKay.49:42
The assist for the goal came from Johnny Hayes.49:42 Goal scored
Goal! - Josh Magennis - Aberdeen 2 - 1 Inverness Josh Magennis grabs a goal from the edge of the penalty area to the bottom right corner of the goal. Aberdeen 2-1 Inverness CT.48:05
Johnny Hayes produces a left-footed shot from just outside the box that goes wide right of the goal.47:39
Inswinging corner taken by Johnny Hayes, clearance by Gary Warren.46:56
Graeme Shinnie concedes a free kick for a foul on Johnny Hayes. Free kick taken by Johnny Hayes.45:01
The match restarts for the second half.45:00 +3:19 Half time
Half Time The half-time whistle blows.45:00 +2:38
Free kick awarded for an unfair challenge on Gary Warren by Rory Fallon. Free kick taken by Josh Meekings.45:00 +1:52 Booking
Booking Booking for Josh Magennis for unsporting behaviour.45:00 +1:04
The assist for the goal came from Johnny Hayes.45:00 +1:04 Goal scored
Goal! - Josh Magennis - Aberdeen 1 - 1 Inverness Josh Magennis gets on the score sheet with a goal from close range low into the middle of the goal. Aberdeen 1-1 Inverness CT.42:37
Mark Reynolds takes the direct free kick.42:37 Booking
Booking Gary Warren is given a yellow card.42:32
Gary Warren challenges Russell Anderson unfairly and gives away a free kick.41:10
Unfair challenge on Rory Fallon by Richie Foran results in a free kick. Free kick taken by Russell Anderson.40:36
Andrew Considine gives away a free kick for an unfair challenge on Richie Foran. Direct free kick taken by Josh Meekings.39:46
The ball is crossed by David Raven, Mark Reynolds makes a clearance.38:38
The referee blows for offside. Free kick taken by Antonio Reguero.37:30
David Raven has a shot on goal from outside the area which misses right.35:41
Assist by Aaron Doran.35:41 Goal scored
Goal! - Billy McKay - Aberdeen 0 - 1 Inverness Billy McKay gets on the score sheet with a goal from inside the six-yard box low into the middle of the goal. Aberdeen 0-1 Inverness CT.35:34
Aaron Doran crosses the ball,34:37
Shot by Scott Vernon from a long way out goes over the net.33:03
The ball is delivered by Aaron Doran, save by Jamie Langfield.31:16
Russell Anderson delivers the ball, David Raven makes a clearance.30:23
Jamie Masson gives away a free kick for an unfair challenge on Andrew Shinnie. Direct free kick taken by Owain Tudur Jones.29:23 Substitution
Substitution Jamie Masson joins the action as a substitute, replacing Chris Clark.25:35
Free kick taken by Owain Tudur Jones.25:35 Booking
Booking Chris Clark is cautioned.25:27
Chris Clark concedes a free kick for a foul on Aaron Doran.24:39
Philip Roberts challenges Josh Magennis unfairly and gives away a free kick. Russell Anderson takes the free kick.21:27
Russell Anderson gives away a free kick for an unfair challenge on Andrew Shinnie. Direct free kick taken by Andrew Shinnie.18:44
Unfair challenge on Russell Anderson by Billy McKay results in a free kick. Jamie Langfield takes the free kick.16:45
Foul by David Raven on Rory Fallon, free kick awarded. The free kick is delivered left-footed by Johnny Hayes from left wing, Gary Warren makes a clearance.15:16
Free kick awarded for a foul by Andrew Shinnie on Johnny Hayes. Direct free kick taken by Johnny Hayes.13:53
Inswinging corner taken by Johnny Hayes, Owain Tudur Jones manages to make a clearance.13:39
The ball is sent over by Johnny Hayes, clearance made by Josh Meekings.13:17
Short corner worked by Johnny Hayes.13:02
Volleyed left-footed shot by Johnny Hayes. Owain Tudur Jones gets a block in.12:17
The official flags Philip Roberts offside. Jamie Langfield takes the free kick.11:58
Niall McGinn takes a shot. Antonio Reguero makes a comfortable save.11:08
Unfair challenge on Richie Foran by Rory Fallon results in a free kick. Direct free kick taken by Antonio Reguero.9:01
Corner taken left-footed by Graeme Shinnie, Foul by Philip Roberts on Mark Reynolds, free kick awarded. Free kick taken by Jamie Langfield.6:49
Joe Shaughnessy concedes a free kick for a foul on Richie Foran. Antonio Reguero takes the free kick.5:31
Effort on goal by Josh Magennis from 6 yards. Save by Antonio Reguero.4:46
Free kick awarded for an unfair challenge on Andrew Shinnie by Chris Clark. Direct free kick taken by Josh Meekings.4:03
David Raven gives away a free kick for an unfair challenge on Joe Shaughnessy. Johnny Hayes crosses the ball in from the free kick, Graeme Shinnie makes a clearance.2:23
Rory Fallon fouled by Owain Tudur Jones, the ref awards a free kick. Direct free kick taken by Josh Magennis.2:10
Graeme Shinnie crosses the ball, Mark Reynolds manages to make a clearance.0:00
The referee gets the game started.
Live data and text provided by our data suppliers