With patents the deal is as follows: Tell us how you made it and we will make sure that noone else copies it for a while. This system allows for knowledge to be shared between competing entities. One of the requirements for a patent is literally "The descriptions must be sufficient to enable any person skilled in the art to which it pertains to make and use the same".
For medication, the reason why it can even go off-patent is because the patent exists in the first place. Without the patent, there would be no public information about how to make the medication.
I'm not saying that this is the case for all inventions. But I do think that patents have their place. The current form might however need revisiting.