This is a widely reported scam on Discord and Minecraft, where the scammer ask you for that code to join their server or realm, that code gave them full access to your account and they immediately change the recovery details like the associated Email to block you from accessing the account.
I wish I had better news, but when a Microsoft account is hacked, if the hackers change the associated Email, then that account is usually no longer recoverable, this forum is just a user to user account and no-one here has any account access, try using the process they provide on this link to recover a hacked account, if this process is not successful, sadly, that account cannot be recovered.