This marks the first time Milioti's character, identified only as "the girl with the yellow umbrella" at the end of Monday night's episode, has appeared on the show.
CBS confirmed Milioti -- former star of the Broadway musical "Once" -- will play the future wife of Josh Radnor's character, Ted.
Shot March 27 at the Twentieth Century Fox lot in Los Angeles, Milioti's casting and filming of her scene was kept in strict secrecy in order to preserve the surprise for the audience, producers said.
"It was a pretty big umbrella to fill. Casting the title role in a series that's been on the air for eight years," Carter Bays, the show's executive producer and co-creator, said in a statement Tuesday.
"And yet Cristin Milioti is, against all odds, exactly what we were looking for. She made us laugh on '30 Rock,' she made us swoon in 'Once,' and her ukulele skills are no joke. We're thrilled to welcome her to the 'How I Met Your Mother' family, and look forward to getting to know her -- and the character she plays -- much better in the coming final season. And to all the fans, on behalf of all of us writers, cast and crew: Thank you for sticking with us on this wild journey. Our ninth season will tell the epic story of the longest wedding weekend ever. We hope you like it. Next stop, Farhampton."
The show is the very long story Ted tells his children in 2030 about his life before he became a family man.