The origin of the doctorate degree dates back to the ijazat attadris wa 'l-ifttd ("license to teach and issue legal opinions") in the medieval Islamic schools from the 9th century, though it was limited to Islamic law at the time, as in a Doctor of Laws degree.
The doctorate was later extended to philosophy in European universities in the Middle Ages which generally placed all academic disciplines outside the professional fields of theology, medicine and law under the broad heading of "philosophy" (or "natural philosophy" when referring to science). The degree of Doctor of Philosophy was generally granted as an honorary degrees to well-established scholars.
According to Wellington, Bathmaker, Hung, MucCullough and Sikes (2005), the first Ph.D. was awarded in Paris in 1150; but not until the early nineteenth century did the term "Ph.D." acquire its modern meaning as the highest academic doctoral degree, following university practice in Germany.
The authors explain that prior to the 19th century professional doctoral degrees could only be awarded in theology, law or medicine. In 1861, Yale University adopted the German practice of granting the degree to younger students who had completed a prescribed course of graduate study and successfully defended a thesis/dissertation containing original research in science or in the humanities.
From the US the degree spread to Canada in 1900, and then to the UK in 1917. (info from Wikipedia, photo from Univ. of North Carolina)