The result - which saw the party trail in fourth place behind the Greens and lose its £500 deposit - came on the back of the crushing defeat in Hartlepool last month.
In his departure email to staff, Mr Nunn continued to back the Labour leader despite the setback but said he wanted to "go forward and do something different".
"I remain as convinced today as I ever have been that he will be a great prime minister and make this a better, stronger, more prosperous country," he wrote.
"The decision to step down has been one of the most difficult decisions I've ever had to take."
Mr Nunn, who has worked with Sir Keir since 2017, added: "In that time I've worked through the junior doctors' dispute, EU referendum campaign, a leadership contest, Brexit, a general election, another leadership contest and a global pandemic.
"I've had the opportunity to learn and experience so much. Now is the chance for me to go forward and do something different."
Deputy communications director Paul Ovenden also stood down - but his departure was understood to be because of family reasons and unconnected to political events.
Thursday's by-election saw the Liberal Democrats overturn a Tory majority of 16,000 to seize the Conservative stronghold by 8,028 votes.
Chesham and Amersham had belonged to the Tories since its creation in 1974.
Polling expert Sir John Curtice told BBC Radio 4's Today programme that he made it the "worst Labour performance in any by-election".
Additional reporting by Press Association