Rover, an 11-year-old collie-labrador cross, sniffed out the cub while on a walk with owner Steven Miller in the grounds of Edinburgh Royal Infirmary last Thursday.
He refused to let Mr Miller leave one area, where his owner discovered the distressed fox.
Animal rescue officer Steph Grant, from the Scottish SPCA, arrived to free the ten-week-old male and took him to the charity’s National Wildlife Rescue Centre in Clackmannanshire, where he was named Jacob.
Ms Grant said: “The overflow pipe that Jacob had become trapped inside was extremely narrow and there is no way he would have been able to get himself out.
“Thankfully he didn’t have any injuries.
“We don’t know how he managed to get himself into the pipe, but one thing is for sure, Rover is a true canine hero for finding him.”
Centre manager Colin Seddon said: “Jacob was very lucky to have been discovered as he would have perished otherwise.
“He’s coming on really well and is now eating solid food.
“We’ve introduced him to three other fox cubs in our care and we are hopeful that they will all be released at a carefully-selected site when they are at the right age and stage to be able to fend for themselves in the wild.”