But the never-say-die Queenslanders dug deep after the break with the first stirrings of an unlikely win coming when Gagai finishing off a long-range movement to score his first try in the 53rd minute.
A run of penalties and refereeing decisions helped the visitors to keep the pressure on throughout a heart-stopping last quarter but the Blues looked to have done enough before Queensland were awarded a scrum feed near the line.
Conjuring memories of Mark Coyne's famous 1994 game one match-winner - and so many others - Gagai was again on hand to latch on to a Michael Morgan pass on the end of a backline spread out to the right to spoil the Blues plans of celebration.
The comeback came after the Maroons opened the scoring on their second visit into attacking territory with Holmes diving in to score in the corner and Thurston curling the touchline conversion between the uprights after 11 minutes.
Consecutive penalties gave the Blues momentum and field position in the lead-up to their reply through Hayne, before five-eighth James Maloney equalled his Maroons opposite in nailing the sideline kick to draw level after 15 minutes.
The Blues then put the pedal down with two tries in three minutes, with Maloney bursting down the short side past debutant Queensland front-rower Tim Glasby to send Morris over, before fullback James Tedesco broke clear in the first set off the kick-off to put Pearce in beside the posts to extend their lead to 10-points after 28 mins.
Queensland's attack suffered when Thurston took a knock to the injured shoulder that kept him out of game one and two crucial try-saving tackles from New South Wales bench forward Jake Trbojevic on Cooper Cronk and centre Josh Dugan on Michael Morgan prevented the visitors from narrowing the margin before halftime.
The Maroons looked short on ideas early on in the second-half but kept their belief before reward came in familiar fashion.
NSW 16 (Jarryd Hayne, Brett Morris, Mitchell Pearce tries, James Maloney 2 cons, pens)
Queensland 18 (Valentine Holmes, Dane Gagai 2 tries, Johnathan Thurston 4 cons, pens)