Trinity led 10-0 at one stage and were six points ahead at the interval, but seemed set to suffer second-half heartbreak for the third successive game this season when Saints went 12-10 up.
The hosts scored three tries in 20 minutes either side of the break, but crucially Mark Percival – who scored two of them – could not land a goal.
With five minutes left referee James Child went to the screen for a possible touchdown by Jacob Miller from Sam Williams’ kick between the posts.
Child signalled no try and the Trinity stand-off clearly lost the ball. But video official Phil Bentham awarded a penalty try for a push by defender Danny Richardson as the Wakefield man tried to touch down, Williams converted and Wakefield held on for a famous victory.
PICTURE FOCUS: Ossett United open their season with FA Cup tie win
Wakefield trailblazer Michelle Holmes is delighted to see growth of women’s football
Wrenthorpe cycling star Ollie Wood shakes off illness to win Commonwealth Games medals
South Elmsall's Lewis Daykin defies injuries to make winning bareknuckle boxing debut
Commonwealth Gold is extra special for James
Wakefield’s performance before the interval was a model of how to play wet weather rugby.
In driving rain and with the pitch cutting up from the start, their handling was safe, they kicked well, enthusiastic defence pressured Saints into errors and took points whenever they were on offer.
The visitors were full value for their big lead, but – as Wakefield’s lost their discipline, conceding a series of soft penalties – Saints seemed to have pulled the game from the fire.
It was a huge effort from Wakefield, who lost Anthony England just before kick-off, forcing a reshuffle which saw Tinirau Arona start and Anthony Walker come in on the bench.
Trinity’s players put their body on the line time and again, none more so than captain Danny Kirmond, who twice had to be helped from the field.
Williams got Wakefield on the board through a penalty goal and they struck again at the end of the first quarter.
With Ben Jones-Bishop ruled out, Mason Caton-Brown came into the team for his debut and produced an outstanding finish, diving over at the corner from a pass by Scott Grix. Williams’ touchline conversion, into the rain, was just as good and the stand-off extended the lead further with a second penalty.
Saints were poor for most of the first half and it took them 35 minutes to get into Wakefield’s 20, but they scored on their first serious attack.
Williams made a good tackle on Adam Swift, but took too long to get off and was penalised. Louie McCarthy-Scarsbrook was held up over the line by David Fifita in the subsequent set, but on the next play, the ball was moved right and Ryan Morgan crossed from Tommy Makinson’s pass four minutes before the break.
Makinson, at full-back in place of injured England man Jonny Lomax, turned try-saver in the final minutes of the half, pulling down Kyle Wood after he had cut through.
Trinity defended magnificently to survive back-to-back line drop outs at the start of the second period, but that hard work was undone when Fifita was penalised near his own line, for moving off the mark at a play-the-ball and quick Saints handling finished with Makinson sending Percival over.
He crossed again soon afterwards in the set after another penalty conceded by Fifita, for a dangerous tackle on Fages, but Wakefield refused to let their heads drop and deserved their win in the end.
St Helens: Makinson, Owens, Morgan, Percival, Swift, Fages, Richardson, Walmsley, Roby, Thompson, Wilkin, Peyroux, Knowles. Subs Amor, McCarthy-Scarsbrook, Adam Walker, Taia.
Wakefield Trinity: Grix, Johnstone, Lyne, Tupou, Caton-Brown, Miller, Williams, England, Wood, Huby, Ashurst, Kirmond, Sio. Subs Finn, Anthony Walker, Fifita, Batchelor.
Referee: James Child (Batley).