Why am I getting dark black photos in P, S, and A mode on Nikon D5200?

Your correct image shows: ƒ/5.6, 1/500, ISO 800.

Your underexposed image shows: ƒ/9.0, 1/4000, ISO 400.

Your EXIF data shows a -5 EV Exposure Bias which seems to be correct, looking at your photos. This is most probably coming from the exposure compensation you applied (using the +- icon). All the other settings look okay.

Exposure compensation might stay unaltered if you do not do the proper reset procedure.

Check this video or your User Manual out for further info.

(Please let us know if this was the reason.)