The origins aren't known exactly, as it turns out.
Court dress goes back quite some time. Edward III - living up to the example of Edward I, and his legal improvements - was not the first to mandate that judges wear appropriate attire, but it became established during his reign. Judges of importance wore robes lined with fur and silk. Their attire also included a hood and cowl, as well as a mantle. Colors varied, with violet being the color of choice in the winter green in the summer.
In medieval times, the coif, a white cap, was used by monks. Skullcaps (in black) were adopted later on, but wigs became the head covering of choice in the late 17th and early 18th centuries, largely because they became part of the formal clothing worn at the time - not just inside courtrooms.
Court and Tribunals Judiciary
Wigs, Coifs, and Other Idiosyncrasies of English Judicial Attire