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.
A Magennis double temporarily put Aberdeen ahead
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.
- 01 Langfield
- 04 Anderson
- 06 Considine
- 22 Reynolds
- 23 Shaughnessy
- 07 Clark Booked (Masson - 30' )
- 10 McGinn
- 11 Hayes (McManus - 87' )
- 09 Vernon Booked
- 14 Fallon
- 21 Magennis Booked
- 32 Brown
- 24 Brown
- 25 Masson (Smith - 80' )
- 35 Murray
- 37 O'Neill
- 34 McManus
- 38 Smith
Inverness Caledonian Thistle
- 12 Reguero
- 02 Raven
- 03 Shinnie
- 05 Warren Booked
- 14 Meekings
- 04 Tudur-Jones
- 10 Shinnie
- 07 McKay
- 09 Foran
- 17 Doran
- 23 Roberts (Draper - 60' )
- 31 Mathieson
- 32 Cooper
- 08 Draper
- 19 Morrison
- 21 Polworth
- 22 Pepper
- 16 Oswell