My XML sitemap returns a 404 error

When you enable XML sitemaps, or change some options related to XML sitemaps, you have to flush your permalinks (ie: updating URL rewriting).

To do that, just click the Flush permalinks button in SEO > XML / HTML Sitemap.

Or, go to Settings > Permalinks. You don’t need the save the page to flush your permalinks. It’s done each time you visit this page.

Also, make sure to never cache XML sitemaps. Usually, caching plugin like W3Total Cache or WP Rocket automatically exclude this kind of files.