In The Ultimate Foe, the Master appeared in the Matrix, revealing that it was possible to infiltrate it. Sabalom Glitz and the Doctor's future companion Melanie Bush were presented to the Court to rebut the Valeyard's accusations. It was then revealed that the Valeyard was, in fact, the Doctor himself — or rather, a distillation of the Doctor's evil side, a potential dark version who might exist around his twelfth and final incarnation. This concept is similar to the ethereal "Watcher" that manifested itself to bridge the gap between his fourth and fifth incarnations (Logopolis).
The Valeyard was also revealed to be acting at the behest of the High Council of Time Lords to cover its corruption in the Ravalox affair. The "secrets" were information from the Matrix. Ravalox was Earth, but the Time Lords moved it through space, killing virtually every human being living on it. To prevent the Doctor discovering the secret and revealing it, they used the Valeyard to try to have the Doctor executed under the pretence of a trial. The reward for the Valeyard's actions would have been to give him all of the Doctor's remaining regenerations and make his existence concrete. However, the Valeyard would then have slain every member of the Court as well, using a particle disseminator located within the Matrix.
The Doctor entered the Matrix and fought and defeated the Valeyard in a fictional world of his creation. The Inquisitor revealed that Peri had indeed survived and was married to Yrcanos. The Master and the Valeyard appeared to be trapped in the Matrix, with the Valeyard apparently being destroyed by the feedback from the particle disseminator, but at the end of the serial, the Valeyard was seen disguised as the Keeper of the Matrix. The subsequent whereabouts of the Valeyard have never been disclosed in the television series.