As far as I remember, this was mentioned in the BBC series "Victorian Pharmacy" (made in 2010). Many pharmacists made their own patent medicines, including pills, which could be bought over the counter without the need for a prescription. A "doctor's prescription" merely meant that the doctor had said "Give him some liver pills" (or whatever). And before the NHS, there was no such thing as a free prescription, and the doctor had to be paid. Much cheaper just to ask the local chemist.
Many companies also manufactured pills: Beecham's Pills were first marketed in 1842.
Many of the chemist-made remedies contained opium or cocaine: I suspect that the decline would also be linked to government regulation of medicines.