"Counter" refers to a table over which business is done, in this case buying and selling medicines (as opposed to prescribing them) but the term can be used for any purchase of goods. To find the detailed history, I had to go to The Oxford English Dictionary, which says under counter, meaning 4 a:
A banker's or money-changer's table; also, the table in a shop on which the money paid by purchasers is counted out, and across which goods are delivered. The tradesman stands behind the counter; goods are sold and money paid over the counter.
This meaning of "counter" goes back to the 17th century. The oldest sense of the word "counter" was for a token used in accounting, and later in playing games. The meaning of a table for doing calculations followed soon after, by about 1500. This expanded to refer to tables in banks over which business was done, and later to tables in shops.
Rference: "counter, n.3". OED Online. March 2023. Oxford University Press. (accessed June 19, 2023)