Andy Griffith, who illuminated the charming dignity of small-town Southern life with his performance as a kindly sheriff in the popular and enduring 1960s sitcom “The Andy Griffith Show” and who decades later remained a TV favorite as the canny courtroom lawyer in “Matlock,” died July 3 at his home in Manteo, N.C. He was 86 and had a cardiac ailment.

His death was confirmed by his friend William Friday, a former president of the University of North Carolina.

With his lanky build, boyish smile and winsome drawl, Mr. Griffith was one of the most recognizable figures on television for more than five decades.

During cultural turmoil, political assassinations and war in real America, he kept a comforting sort of order in Mayberry, although order was not hard to keep. Mayberry was a place where Sheriff Andy Taylor wore a badge but normally holstered no gun, where the local drunk turned himself in after drinking too much, and where a pickle-making contest qualified for a ruckus.

Thanks to syndication, “The Andy Griffith Show” has been in continual circulation since its original run and dominated Mr. Griffith’s long and varied career. He broke into the entertainment industry in the early 1950s with a country bumpkin monologue called “What It Was Was Football” and then starred on Broadway as a hapless Southern draftee in the hit comic play “No Time for Sergeants” (1955).

Mr. Griffith also starred in the 1958 film version of the Broadway play, but he showed dazzling potential as a dramatic performer in the 1957 movie “A Face in the Crowd,” directed by Elia Kazan and with a script by Budd Schulberg.

Mr. Griffith played Larry “Lonesome” Rhodes, a Southern vagrant-turned-television demagogue who is ultimately undone by his megalomania. The film has been venerated as a prescient look at the future of television and politics.

Mr. Griffith said that Kazan and Schulberg — the team behind such powerful films as “On the Waterfront” (1954) with Marlon Brando — were initially skeptical about casting him in “A Face in the Crowd.” He said they figured that he was not suited to the part because he was so good at playing likable hillbillies and seemed perhaps a bit too nice offstage as well.

Mr. Griffith said he was determined to prove he could play Lonesome Rhodes. He convinced Kazan by doing an impersonation of evangelist Oral Roberts conducting a “healing” of the director. Kazan, Mr. Griffith told the Los Angeles Times, “hired me the next day. . . . At that moment, he and Budd could both see that I had a little wild side — that is, I can create a wild side.”

He added that Kazan used that side “to find the emotions of evil, the various thousands of moods that this man had.”

But the role took a toll on Mr. Griffith. To play the part, he harnessed all the heartbreak and sting from having been called “white trash” while he was growing up in rural North Carolina. He took to smashing things, to feel Lonesome’s anger. He said that digging so deeply into the character, working in the Method acting style favored by Kazan, affected his marriage.

“I’ll tell you the truth,” he once told the New York Times. “You play an egomaniac and paranoid all day, and it’s hard to turn it off at bedtime. We went through a nightmare.”

Mr. Griffith soon became a homespun staple on television. His Andy Taylor of Mayberry, a steady-handed widowed sheriff, was born in an episode of “The Danny Thomas Show” in 1960 and that same year was spun off into “The Andy Griffith Show” on CBS.

The sitcom’s co-stars included Ron Howard as Andy’s son, Opie; Don Knotts as his easily frazzled deputy, Barney Fife; and Jim Nabors as gas station attendant Gomer Pyle.

As originally envisioned by producer Sheldon Leonard, “The Andy Griffith Show” featured Mr. Griffith as the comedic star of the program. “It was about a fella who was a sheriff of the town and justice of the peace and editor of the paper and he told funny stories and he had an aunt and a boy,” Mr. Griffith told the Toronto Star. “I never did like the first show. I thought it was very soft.”

The turning point was Mr. Griffith’s decision to emphasize Knotts and his comedic genius.

“That was golden for us,” Mr. Griffith told the Star, “that Barney should be the comic and Andy should be the straight man. And after that we added the other comedic characters, and it changed the nature of the show, and I gave us a chance to last as long as we did.”

By the end of its run in 1968, “Andy Griffith” was No. 1 in the Nielsen ratings. More than half a century later, most Americans who watched the original show can whistle its theme song on command. Their children and grandchildren know the tune from the ever-present reruns.

Mr. Griffith studiously avoided saying that Mayberry was modeled on his home town. But to many fans — and to visitors at the Andy Griffith Museum that now stands there — it was.

Andrew Samuel Griffith was born June 1, 1926, in Mount Airy, a town in the foothills of North Carolina.

He was musically inclined from a young age and counted as a pivotal moment watching trombonist Jack Teagarden showcased in the 1941 musical film “Birth of the Blues.” Mr. Griffith got a janitorial job to buy a trombone. His 1996 album “I Love to Tell the Story: 25 Timeless Hymns” won a Grammy Award.

He variously expressed career interests in opera singing and the study of divinity. He graduated in 1949 from the University of North Carolina at Chapel Hill with a music degree and was appearing on the dinner-club circuit when he conjured up the football sketch that would rocket him to fame as a corn-pone comic.

After “The Andy Griffith Show” ended, Mr. Griffith appeared in the short-lived spinoff “Mayberry R.F.D.” and starred in a run of television crime movies that led to his most notable late-career role, in the legal drama “Matlock.” The show, which often depended on last-minute courtroom revelations orchestrated by Mr. Griffith as a crafty defense attorney, was one of the most popular dramas of its era. It ran from 1986 until 1995, first on NBC and later on ABC.

His first marriage, to Barbara Edwards, and his second marriage, to Solica Cassuto, ended in divorce. A son from his first marriage died in 1996. Survivors include Cindi Knight, whom he married in 1983, and a daughter from his first marriage.

In one popular episode of “Andy Griffith,” when Opie wallops a bird with his slingshot, Andy makes the boy raise its offspring before releasing them into the wild.

“Cage sure seems awful empty, don’t it Pa?” Opie says.

“Yes, son, it sure does,” Mr. Griffith responds to the boy. “But don’t the trees seem nice and full?”