By Lynette Rice
Updated September 22, 2015 at 03:31 PM EDT
Credit: Phil McCarten/AP

Even Tina Fey didn’t know that she was about to do the Emmys with her old pal Tracy Morgan.

While the Unbreakable Kimmy Schmidt creator was preparing to present the award for best actor in a drama, someone came up to her and said Morgan would like to say hello. Fey’s first reaction: What?

“We both got very emotional,” Fey told EW after Sunday’s telecast. “We were hugging and stuff. I was glad he was able to right himself and go out [and speak]. I was so happy there was a joke in there!”

It turns out that Morgan’s surprise appearance at the end of the ceremony was months in the making. EW learned that executive producer Don Mischer had been working with the comedian’s reps since June to bring him to the Microsoft Theater. Having been in recovery after a serious car crash over a year ago, Morgan has largely remained out of the spotlight but the 30 Rock star “loved the idea of surprising everyone and he was very appreciative of Don’s team and Fox working so hard to let it unfold the way he envisioned it,” his publicist Lewis Kay said.

WANT MORE EW? Subscribe now to keep up with the latest in movies, television and music.

To protect their big secret, Mischer and his team only used code names to refer to Morgan. It all paid off: Morgan’s speech was both touching and hilarious. “Last year, Jimmy Kimmel stood on this stage and said, ‘We’ll see you back here next year, Tracy Morgan.’ Well, Jimmy, thanks to my amazing doctors and the support of my family and my beautiful new wife, I’m here. … Only recently I’ve started to feel like myself again,” he added, “which means a whole lot of women are gonna get pregnant at the afterparty.”

Fey was proud of her old buddy. “I really thought he was going to be sincere and everybody loves him and then he dug out a joke,” Fey said. “He’s really been through something terrible. For him to have survived this is nothing short of a miracle.”