NBC has rescued acclaimed but low-rated comedy “Brooklyn Nine-Nine,” a day after Fox canceled the series, sparking a fan outcry on social media.

The series, wrapping up its fifth season May 20, will return for 13 new episodes (down from 22 on Fox), but likely won’t resurface until early 2019.

NBC’s Universal Television owns the comedy, which stars Andy Samberg and Andre Braugher and is set in a police precinct, so extending it figures to increase profits on the show and give the network some goodwill among comedy fans of its like-minded “Superstore” and “The Good Place,” whose creator, Michael Schur, also created “Brooklyn” along with Dan Goor.

Schur will have a third show due on NBC next season, a comedy called “Abby’s.”

The cast and producers tweeted simultaneously about the news late Friday night; NBC will announce its schedule Sunday and unveil it to advertisers Monday at Radio City Music Hall.

Samberg (@thelonelyisland) and Melissa Fumero were among the cast members celebrating on Twitter.

“Ever since we sold this show to Fox, I’ve regretted letting it get away, and it’s high time it came back to its rightful home,” said NBC Entertainment chairman Robert Greenblatt, in a statement. “Mike Schur, Dan Goor, and Andy Samberg grew up on NBC, and we’re all thrilled that one of the smartest, funniest, and best cast comedies in a long time will take its place in our comedy lineup.”