Skating in front of a crowd of euphoric supporters at the Gangneung Ice Arena, the 23-year-old took steps out of his quad toeloop and triple Lutz. But those errors did not stop him from delivering a gold-worthy performance.
Hanyu, a two-time world champion, finished nearly 11 points overall ahead of compatriot Shoma Uno.
Double Olympic champion Hanyu to miss worlds
"Getting to this place was really tough," Hanyu told reporters. "Because I had time off from skating, I was able to do a lot of planning and thinking about strategy."
Skating last, Uno fell on his opening quad loop but fought his way back to silver, earning a combined total of 306.90 points.
Yuzuru Hanyu, Shoma Uno y Javier Fernández | PyeongChang 2018
Image credit: Getty Images
Javier Fernandez, a six-time European champion competing in his last Olympics, made a wobbly landing on his quad Salchow early in his programme but regrouped to earn 197.66 points for his free skate and 305.24 overall.
"I know I didn't do the perfect programme, but I was satisfied with what I did," Fernandez told reporters.
"It got me an Olympic medal. That's all I've got to say."
Quadruple jump specialist Nathan Chen rebounded from an error-laden short programme with a historic free skate that put him in the lead before Hanyu, Uno and Fernandez had skated.
Image credit: Eurosport
The 18-year-old American, who was 17th after the short programme, became the first skater to land six quads at the Olympics to earn 215.08 points, nearly 11 more than his personal best. He finished fifth with a combined total of 297.35.
"I think that after having such a disastrous short programme and being so, so low in the rankings, lower than almost ever, it allowed me to completely forget about results and just completely focus on enjoying myself out on the ice," said Chen, who won bronze with the U.S. in the team event.
Canada's Patrick Chan, who was sixth going into the free skate after falling on his triple Axel, stepped out of a triple toeloop and put his hand down on a triple Axel in the free skate.
Chan, who won gold with Canada in the team event, finished ninth overall in his last Olympics.
"It was obviously not the dream skate, but I think...from beginning to end, from day one of the Games to now, I'm very, very happy with how I held it together," Chan told Reuters.
"I just kept chugging along at goals and achieved the goals I had set myself."