Madrid has lots and lots of 24 hour pharmacies--many more than any American city that I ever lived in--especially in the center of town. You can find a list of them in the newspaper. ...
I'm not sure if it's required, but in most Cities in Spain
Taxistas will carry a list of
Farmacias de Guardia for any given day. I needed one in Malaga once and my wife just asked a
taxista to take her to the nearest
Farmacia de Guardia.. The
Farmacia de Guardia designation is not necessarily a permanent thing. I believe they rotate that duty.