This is really two questions.
- What is the historical reason for patent protection to be 20 years?
- What is the reasoning for it to be 20 years, instead of 15 years, or 25 years?
This is really two questions.
When the Trade Related Intellectual Property Rights (TRIPs) agreements were being discussed, various countries had their view on what the length of a patent should be. These negotiations lead to 20 years. While there is no hard and fast rule for a length to be 20 years, this was the consensus on which member countries agreed. A detailed overview of TRIPs agreement can be found here: https://www.wto.org/english/tratop_e/trips_e/intel2_e.htm
To answer your second question, the reason is related to economics of innovation. Innovation as we know is good for the welfare of a society. Firms/Innovators can innovate their product by themselves (to differentiate themselves from others or reduce the production cost) or can be incentivized by the society. In a society with no patent rights, incentives to innovate is low. If one firm innovates their product, others can just replicate the innovation for free. The R&D expenditure thus becomes redundant for the innovative firm. Therefore, a society agrees upon patent rights which are incentives to innovate. However, it comes with a cost; it leads to monopoly (one firm with rights to a product and other firms cannot produce the same product without permission from the original innovator). We as a society agree on rewarding an innovative firm with a brief period of monopoly in which they can sell their product at a higher price. The total welfare generated from innovation is greater that no innovation even after considering the costs a monopoly imparts on a society. Nordhaus (1969) and Scherer (1972) have shown theoretically and geometrically that a finite patent length leads to higher welfare than infinite patent length. Intuitively, if patent length is finite, firms will compete to produce a better product by improving on the existing patented product since they only have about 20 years and then the patent rights would expire. On the other hand, if the patent length is infinite, the firm which had a patent on one of its product will sit on the its invention till someone invents a better/cheaper substitute. This will lead to lower effort from the firm which has at least one patent. Therefore, a finite patent length is better than an infinite patent length. Lester and Zhu (2019) summarizes the discussion on TRIPs as commented by @Eric S.
I found an interesting article from the Cato institute about patent term. It covers the history and suggests that the original 14 year term in England (and in the U.S. 1790–1835) had something to do with the length of an apprenticeship. There was some suggestion that in return for the monopoly one needed to teach others to make the invention. At the end of the apprenticeship, and monopoly, they could make it more widely available.
It also covers the debate among economists about the ideas of having different terms for different technologies. With TRIPS this is all moot for the time being. One quote is from a book by Alex Tabarrok -
Patents should be stronger in industries with high innovation-to-imitation costs such as pharmaceuticals and weaker in industries with low innovation-to-imitation costs such as software. Patents of say three, 10 and 12 years could be offered with the divisions either based on industry—with software and business-method patents getting three years, pharmaceuticals 20 years, and other innovations 10 years—or based on evidence of sunk costs. An innovator that wanted a three-year patent, for example, need not offer any evidence on sunk costs and would receive a quick response. Innovators applying for 10- and 20-year patents would have to provide more information and would need to pass a higher hurdle.