This might be an answer too late, however for future users as myself who might be looking for the same answer.
If you have a custom login page that is using the Default MasterPage, then you might run into this issue.
Option 1 is to give anonymous users access to the site (Visit Central Admin, click "Authentication Providers" on the "Application Management > Manage web applications" page; select the zone and select Anonymous Access checkbox. Also give the Anonymous Users permissions for Lists and Libraries) -> This might not be what you want though.
Option 2 is to change the masterpage that is loaded for your custom page.
You can do this:
To save you the hustle, you could adopt the page from
It's for SharePoint 2013, but we've been able to use the same on SharePoint 2016 by creating a custom page and porting the SharePoint elements.
(Notice how the masterpage used on that is the error.master :) )