How are emergency room and general practice doctors qualified in diagnosing and prescribing drugs for mental health?

Training. Er docs are trained to stabilize emergencies, which frequently include mental health crisis, but er drs don't generally do ongoing care. Family docs train for several years on the basic management of common mental health issues and in many communities are the most accessible source of care. If needed, both er &fam drs can also refer to or work with psychiatrists &therapists.
Both ER docs. And family practice docs both spend time learning about mental health problems, though certainly not as much as psychiatrists. And in practice, they certainly see patients with mental health problems. Some even have working relationships with psychiatrists for consultation and referral.