document: Media playback is not supported on this device
  O'Sullivan, 38, was outplayed by his fellow Englishman in two high-quality opening sessions and was fortunate to trail only 9-7 in the best-of-25 match.
  The next four were shared, before O'Sullivan won three straight frames to move ahead for the first time at 12-11.
  And a break of 113 saw O'Sullivan set up a tie with Shaun Murphy or Marco Fu.
  Barry Hawkins repeated last year's semi-final win over Ricky Walden as he edged past his fellow Englishman 13-11 to reach the last eight, where he will face Dominic Dale or Michael Wasley.
  O'Sullivan, who will play in his 16th World Championship quarter-final, told BBC Sport: "That was the closest I've been to going out here for quite a while. I was never in control of that match at any point.
  "But I was determined to stay in it right to the end, I wasn't going to give in. That's probably one of the most exciting matches I've ever had at the Crucible."
  Perry added: "I'm gutted. I gave it everything I had but I just ran into a genius at the end of the match.
  "When he plays like that, it's hard to see anyone other than him winning the title."
  Coming into the tournament, O'Sullivan was the overwhelming favourite to win his sixth world title and third in a row.
  "That was Ronnie O'Sullivan playing pressure snooker when he needed to. I think he's the best pressure player I've ever seen. The trouble for the other players is that O'Sullivan now does not have any weaknesses in his game. And because he is so strong in every department, it puts huge pressure on his opponents. It's hard for them not to enter the match in a negative frame if mind."
  He cruised through his opening match, beating qualifier Robin Hull 10-4, but faced a far more experienced opponent in world number 15 Perry.
  O'Sullivan was never ahead in the opening two sessions as Perry kept the champion off the table with some big break building.
  But the statistics told the story in the final session, with O'Sullivan upping his pot success rate to 97% and Perry dropping down to 85%.
  The opening four frames of the morning were scrappy and it was Perry who retained his two frame advantage at the mid-session interval.
  But O'Sullivan looked rejuvenated after the break, knocking in breaks of 52 and 53 to pull level.
  The Englishman then took the lead for the first time in the match with a scintillating break of 124 in the 23rd frame.
  And a third century of the match sealed a brilliant comeback win, O'Sullivan punching the air in celebration and waving to the crowd before leaving the arena.
  Also on Saturday, last year's semi-finalist Judd Trump reeled off four straight frames to finish the first session with a 6-2 lead over Ryan Day.
summary: Defending champion Ronnie O'Sullivan reached the World Championship quarter-finals with a hard-fought 13-11 victory over Joe Perry in Sheffield.