Medicine Jack put up a game display to outpoint Peace Envoy in the GAIN Railway Stakes at the Curragh.
The leading pair were separated across the track but though Peace Envoy had the rail, Medicine Jack galloped on resolutely in the centre of the course to come out on top at 6-1.
The early pace was set by Mirdif but there were plenty in with chances in the final quarter-mile.
Peace Envoy looked to hold a narrow lead under Ryan Moore in a bid to give trainer Aidan O'Brien a 13th success in Group Two contest.
However, the Ger Lyons-trained Medicine Jack found more for Colin Keane and got the verdict by half a length.
Dream Of Dreams, trained in North Yorkshire by Kevin Ryan, was third.
It was a welcome change of fortune for Keane, who was involved in a nasty-looking incident earlier on the card when his mount Elusive Heights was brought down.
Lyons said: "He did what we thought he could do. That's not being cocky, we like him but you never know when you have Ballydoyle to deal with.
"We thought if King Electric (stablemate, who finished fourth) could run a race then he had a huge chance.
"We have Psychedelic Funk but we've always said he's not the only one. He went to Royal Ascot but the ground went against him and he ran a great race to finish third as he was the first horse off the bridle.
"This horse is still only half a horse and we were worried he was still not mature.
"I was talking with Colin beforehand and was saying 'have we got this wrong' as King Electric was shorter in the betting but he said 'no'.
"He got a terrible fall earlier in the day and it just goes to show you that it can go one way or the other so quickly in racing. I'm delighted both horses and jockeys were all right.
"I said at Navan that winning a Railway was as important to me as winning a Coventry. They are both Group Twos.
"I made a stupid mistake taking this horse to England (sixth at Newbury on Lockinge day). It was pure greed and I only took him over to have another horse with Endless Drama.
"I don't like standing around all day for one race and it wasn't fair on him to run him but it didn't do him any harm and he learned from it.