NEW YORK – There was never any doubt that, someday, “Law & Order” would come to an end.

But the death sentence handed down by NBC on Friday caught many observers by surprise. Viewers weren’t prepared to say farewell to this beloved TV warhorse. Not this way, with this sort of abruptness. And not with it on the brink of entering the history book as TV’s longest-running drama.

What had been intended as the 20th-season finale, a solid but unexceptional episode, will air May 24 as the series conclusion. This would seem an injustice to the show’s proud legacy. And an unceremonious end for its fans.

Maybe there weren’t enough fans (this season, viewership has averaged 7.3 million viewers, the show’s lowest ever and less than half the number at its height a decade ago). Maybe the show was too expensive to produce. Maybe NBC just thought it was too old.

Even so, “Law & Order” had been considered a TV fixture and a good shot for renewal next season. Then it would be poised to surpass “Gunsmoke,” a CBS western that ran 20 seasons from 1955 to 1975.

That record-breaking feat has been an enduring dream of the series’ creator, Dick Wolf, who not only furnished NBC with this so-called “mother ship” but expanded it into two successful “Law & Order” spinoffs. He also will shepherd the just-announced “LOLA” (short for “Law & Order: Los Angeles”) premiering this fall.

Jeff Gaspin, chairman of NBC Universal Television, praised Wolf on Friday and said the legacy of “Law & Order” will “continue to make an impact like no other series before.”

In its latest incarnation, the show’s ensemble cast consisted of Jeremy Sisto, Anthony Anderson, Linus Roache, Alana De La Garza, Sam Waterston (who joined in 1994) and S. Epatha Merkerson, who, unrelated to the show’s termination, had already announced she was moving on after 16 seasons.

These actors have followed in the footsteps of no fewer than 19 other stars who have filled the show’s half-dozen slots for cops and prosecutors since “Law & Order” began.

The series premiered on Sept. 13, 1990. It was thought to be unusually raw and authentic in how each episode followed the twists and turns of a case, which often dealt with pressing social issues seldom addressed by TV drama.

It had an unusual structure, too. For the first half-hour, detectives tracked down the bad guy. In the second half, prosecutors hauled the accused into court.

The stories were said to be ripped from the headlines, with New York City the show’s true star. Largely shot on the streets of New York, it had a striking look at a time when only one other scripted series, “The Cosby Show,” originated from New York.

“Over the last 20 years, ‘Law & Order’ became a New York City institution,” New York Mayor Michael Bloomberg said in a statement Friday. Noting that “it began filming in the city at a time when few series did,” he thanked Wolf for “helping showcase the city’s depth and versatility as a setting.”

The show has also been a job bank, one especially welcome among actors, whether famed or unknown. Over the years, they included stars or about-to-be-stars such as Julia Roberts, Adam Arkin, Claire Danes, Edie Falco, Jennifer Garner, Philip Seymour Hoffman and Samuel L. Jackson. “Law & Order” hired more than three dozen speaking roles per episode, some 700 in a season.

Starting out, the show’s half-dozen starring roles were filled by men, but that changed in its fourth season with the addition of Merkerson and Jill Hennessy. The female presence helped boost the ratings of the show.