Unfortunately you cannot “undo” a mail deletion.
As such, if you delete an email from your mail list - and, subsequent to this deletion the next email in your message list is then opened, it follows that that any available “undo” action will attempt to reverse the last action performed. In this scenario, the last action to “undo” is not the delete action.
Notwithstanding this description, assuming that you are using an IMAP mailbox, all actions are immediately synchronised to the IMAP server - including email deletions. The only mechanism by which you can “undo” an email deletion, if supported by your email provider, is to log-in to your mailbox using your web browser; you might be able to retrieve recently deleted email from the webmail interface.
I hope this information and suggested workaround are helpful - and potentially resolves recovery of an accidentally deleted email.