NEW YORK (AP) — In its 208th and final episode on Monday, the CBS comedy “How I Met Your Mother” finally revealed the mystery contained in its title and then ended with a twist designed to reward longtime fans.

Actor Josh Radnor’s character Ted meets the mom, played by actress Cristin Milioti, on a rainy train platform after his friends Barney and Robin are married.

But that marriage doesn’t last, and neither does Ted’s – because Milioti’s character Tracy dies after the couple has two children together. And at the end of the one-hour episode, the romance that seemed natural at the show’s beginning, between Ted and Robin, is rekindled.

Creators Carter Bays and Craig Thomas say they knew the plot for the final episode when the series premiered nine years ago. Success kept them stringing along the story, even though part of Monday’s finale was filmed in 2006 for fear that the teen-aged actors who played Ted’s children would become unrecognizable.

The series has been a solid if not spectacular performer for CBS, and its average of 9.8 million viewers this season means it will likely be its most-watched ever. It has never averaged below 8.25 million viewers in a season, the Nielsen company said, and its success in syndication means reruns will likely be seen for many years to come.

Most of the last season has been set in one weekend: Robin and Barney’s wedding, where Tracy was the bass player in the wedding band. The final episode skips through several years, where we learn that the marriage of Robin and Barney (played by Cobie Smulders and Neil Patrick Harris) splits up because of her success as a television correspondent. Barney has a baby through a one-night stand and Marshall and Lily (played by Jason Segel and Alyson Hannigan) have three kids and he becomes a judge.

Ted and Tracy get married after a long engagement and the kids, but she gets sick and dies, a development that many fans speculated upon in recent weeks because of a foreshadowing in an earlier episode.

After that, Ted wraps up the story – how I met your mother – that he had been telling his children throughout the length of the show.

The children, however, tell him that it’s also a story about how he loves their Aunt Robin. They say his attraction to her is obvious and that he should ask her on a date.

“C’mon, dad,” said the daughter, played by Lyndsy Fonseca. “Mom’s gone for six years now. It’s time.”

Ted and Robin dated early in the show’s history and seemed a natural match. Yet the producers made clear that Robin was not the “mother” of the title, disappointing many of the show’s fans at the time.

The children urge Ted to pick up the phone and call Robin. He thinks better of it and drives to her apartment. She opens the window to see him holding a blue horn – a reference to a story from when they had dated – and the series ends.

The producers are at work on a pilot for a spinoff series, “How I Met Your Dad,” that involves different actors. This cast has already scattered, and commercials that aired during the finale advertised an upcoming movie starring Segel and a Broadway show where Harris is appearing. On Monday night, Smulders appeared at a New York screening for the movie “Captain America: The Winter Soldier,” in which she performs.

Smulders said that after the screening, she planned to go home and watch the finale with friends. She said many of the emotions felt by the characters in the last episode mirrored what the actors were feeling.