Stokes's 85-ball ton was the fastest in a Test at Lord's, while Cook batted throughout day four for his unbeaten 153.
Joe Root also made 84 as England moved from 74-2 and a deficit of 60 to 429-6, a lead of 295.
That sets up a potentially thrilling final day, with both sides having a chance of a victory that would give them a 1-0 lead in the two-match series.
Cook, in the company of Moeen Ali, who is unbeaten on 19, may have to decide on a declaration that would leave enough time to bowl New Zealand out, while the tourists will look to wrap up the England innings before embarking on a run-chase.
It was a remarkable turnaround for England, especially after Ian Bell loosely edged Tim Southee's third ball of the morning behind to raise New Zealand hopes of a four-day win.
As the visiting seamers found exaggerated movement, Cook and Root had to withstand an immensely difficult first hour.
They survived to score freely either side of lunch, but in nothing like the thrilling manner in which Stokes flayed the Black Caps attack.
The Durham all-rounder began by powerfully playing through the off side, but the most riveting part of his second Test century was the manner in which he attacked Southee's short-pitched bowling.
In a two-over period, Stokes ignored three fielders on the leg-side boundary to pull three sixes, with Southee conceding 37 runs from 12 balls.
When Stokes then nudged Matt Henry for a single, he completed the fastest Test hundred by an England batsman since Gilbert Jessop's 76-ball century in 1902.
His stand with Cook was worth 132 and ended when Stokes looked to heave the off-spin of Mark Craig but instead edged to first slip.
The England captain contributed only 24 runs, having already shared a vital 158 for the fourth wicket with Root.
After absorbing the pressure of the New Zealand pace bowlers, they cashed in on the inaccuracy of Craig, who too often dropped short.
As he did in moving to 32 on the third evening, Cook continually worked the ball off his pads, but also showed excellent judgement in leaving deliveries outside off stump as well as cutting and pulling.
When he reached 99, opposite number Brendon McCullum posted eight catchers on the off side, but a stab down the ground took Cook to a 27th Test ton and his first in England for two years.
After going 35 innings without reaching a century, the left-hander now has two in his last two Tests.
By then, Root had fallen short of his own hundred for the second time in the match. Circumspect at first - he scored only 12 from his first 45 balls - he busily accumulated behind point to take 40 from his next 38.
The vice-captain seemed set for three figures, but fell into an obvious trap set by Henry, hooking a short ball to long leg.
At that point, England were in the dangerous position of being 98 ahead with six wickets in hand. Then came the Stokes pyrotechnics.
Listen to Geoffrey Boycott and Jonathan Agnew review the day's play on the Test Match Special podcast.
Get wicket alerts, end-of-innings scores and results sent to your mobile from the BBC Sport app.
Ben Stokes's dazzling 101 and a composed hundred from Alastair Cook reversed the momentum of England's first Test against New Zealand.