In general, try to plan ahead since German pharmacies and shops opening times are relatively restrictive. Also note that a lot of medicine is prescription-only, so you will need to get a prescription from a doctor in order to get this kind of medicine.
If you need prescription-free medicine (like painkillers etc.) at night or on a Sunday, you can locate a "Notdienst Apotheke", i.e. a pharmacy which operates outside normal opening hours, via this website: http://www.akberlin.de/notdienst.html
In the first field, "Datum/Uhrzeit", enter the date in the format dd.mm.yyyy.
In the second field, "Uhr", enter the time in the format hh:mm.
In the third field, choose your district from the drop-down menu.
Also, each pharmacy when it is closed will have a note saying which is the nearest "Notdienst Apotheke" or "Nachtapotheke".
If you need urgent medication which is prescription-only (like antibiotics etc.), you'll have to go to a hospital during these times.