The Fourth Doctor and K9 Mark II spent an indeterminate period of time together. However, the Doctor appeared to be activating K9 Mark II, implying that no significant time had elapsed since leaving Gallifrey. (TV: The Ribos Operation) It is possible that the Doctor spent some time at Nest Cottage before activating him. (AUDIO: The Stuff of Nightmares)
K9 Mark I was created about the year 5000 by Professor Frederick Marius, who gave the dog to the Fourth Doctor and Leela. He was left behind with Leela on Gallifrey. (TV: The Invasion of Time) Some time later, he was blown up by a bomb on Gallifrey, (AUDIO: Imperiatrix) but survived. This model came to an end when Alistair Gryffen's Space-Time Manipulator randomly pulled him to London in the year 2050. There, he almost immediately self-destructed to protect Gryffen and a group of teenagers from Jixen warriors. (TV: Regeneration)
K9, retroactively "K9 Mark I" (Leeson), initially appeared in The Invisible Enemy (1977) as the creation of Professor Marius (Frederick Jaeger) in the year 5000. K9 subsequently travelled with the Fourth Doctor (Tom Baker) and Leela (Louise Jameson) as a companion of the Doctor in his adventures in time and space until The Invasion of Time (1978). In this serial, K9 decides to remain on the Doctor's home planet of Gallifrey with Leela. Immediately afterwards, Doctor Who would introduce a second incarnation of K9, played by the same prop; the last scene of The Invasion of Time shows the Doctor unpacking a box labeled "K9 Mk II".