I think that this is a Magento bug, triggered by a specific sequence of web requests. The following is the result of a few hours of debugging and it's only correct to the best of my understanding.
Request 1 (loading the login page without a referrer)
If the customer reaches the login page without having a referer parameter in their url, the action url of the sign-in form on this page will not have a referer parameter either. This means that when the form is submitted, the referer parameter will not be set.
Request 2 (attempt to sign-in with invalid credentials)
Since there is no referrer set, \Magento\Customer\Model\Url::getLoginUrlParams sets the referrer to a default, which is the current url, /customer/account/loginPost, because we are in the middle of a loginPost action. \Magento\Customer\Model\Account\Redirect::prepareRedirectUrl then checks if we managed to log in or not, and, since we didn't, sets the redirect to the login url that now has the default, incorrect referrer in it.
Request 3 (signing in with valid credentials)
Magento has a setting that determines whether to redirect the customer to the account dashboard after logging in. The default setting is No, which means that the customer should be redirected back to the page where they were before the login. Since during the 2nd request the referrer parameter was set to the loginPost url, it will now try to redirect to that URL. But that url is POST only, hence we are sent to a 404 page instead.
Solution
The easiest solution to this problem is to set Redirect Customer to Account Dashboard after Logging in to Yes. This setting can be found in Magento admin, under Stores > Configuration > Customers > Customer Configuration > Login Options. This means that customers will always be redirected to the dashboard after login, which may not be the best customer experience, but it's better than a 404 page.
Edit
This is now fixed in Magento 2.4.4 (https://devdocs.magento.com/guides/v2.4/release-notes/commerce-2-4-4.html#general-fixes):
Shoppers are now redirected back to the login page as expected after a second failed login attempt. Previously, shoppers were redirected to a 404 page after a second unsuccessful login attempt.
This is the PR that fixed it: https://github.com/magento/magento2/pull/32891. They changed the code, so the referrer URL is only saved for GET requests.