The 35-year-old informed his team-mates and ECB officials of his decision on Sunday morning before making an emotional speech in the dressing room.
"This is my last two days as your captain and the last two days representing England," Stokes told the squad, urging them to "give absolutely everything" in his final match.
The news was announced publicly during the afternoon session at Trent Bridge while Stokes was bowling. In fitting fashion, he took a wicket with the very first ball after the announcement, having Zak Foulkes caught at second slip by vice-captain Harry Brook.
Stokes then received a standing ovation from the Trent Bridge crowd and was given a guard of honour by both teams as he returned to the field after tea.
One of England's greatest all-rounders, Stokes will finish his career with 122 Test appearances, 7,273 runs and 252 wickets, becoming only the second player after Jacques Kallis to complete the Test double of 7,000 runs and 250 wickets.
His career produced some of English cricket's most memorable moments, most notably his unbeaten 84 in the 2019 World Cup final that helped England win the trophy for the first time and his stunning 135 not out at Headingley that kept the Ashes alive later that summer.
Stokes also transformed England's Test team after taking over as captain in 2022 alongside head coach Brendon McCullum. Their aggressive 'Bazball' approach revived a struggling side and delivered immediate success, although results tailed off in recent years.
His future had come under scrutiny after he missed the second Test against New Zealand while an investigation into an incident at a London nightclub took place. He was later cleared of any serious wrongdoing and returned for the series decider.
ECB chair Richard Thompson described Stokes as "one of England's greatest-ever cricketers and one of the defining figures of his generation".
England must now begin planning for life after one of their most influential players, with Brook among the favourites to succeed him as Test captain.