IT'S A FEW Gilded-Age weeks after Custer's Last Stand (June, 1876), and the Black Hills of Dakota Territory are "the wildest and the richest place on earth." The riches lie lodged in the streams -- gold nuggets. The wildness simmers in the souls of those drawn to the Black Hills Gold Rush: card sharps and failed farmers, prostitutes and preachers, misfits from the California gold fields and the Civil War. Among them are sportswriter and novelist Pete Dexter's central characters: gunfighter and protection-man Wild Bill Hickock; his longtime partner and friend, Colorado Charley Utter; Charley's young nephew, Malcolm Nash; and Jane Cannary, the self-styled Calamity Jane.

Writing in a piquant third-person vernacular descended from The Adventures of Huckleberry Finn but far earthier than anything printable in Mark Twain's day, Dexter interweaves their stories with those of dozens of lesser characters. At first the spotlight is on the impressionable Malcolm, who over the years has made Bill into his Shanelike hero -- "The boy would have worn carrots in his hat if Bill did." Now almost grown-up, Malcolm gets to join Charley and Bill as escorts for a wagon train full of whores bound for Deadwood. En route Malcolm finds his way into one of the wagons and emerges with "a new purpose in life."

Bill himself is suffering from a venereal disease that transforms the simple act of urination into a project. Applying mercury as a treatment, he looks older than his 39 years. He has made a policy of encouraging the tall tales that follow him -- good for business. As for the killing, "There was something in him that turned cold in a fight, and he would kill what was in front of him without a thought, and walk away from it afterwards like it wasn't his business. It was a kind of purity."

Charley maintains an almost proprietary interest in Bill -- so much so that he stumbles into the classic best-friend's pitfall: when Bill acquires a new pal, Charley regards it as treachery. To be sure, the newcomer is a ninny, Captain Jack Crawford, who, on the night they all meet in a bar, tells "the account of his own injuries at Spotsylvania three different times . . ., which to Charley was unforgivable for a man drinking milk."

A few days later Captain Jack talks Bill into going moose-hunting with him. Against his better judgment, Charley accompanies them. Having killed their quarry, they must ferry it across a river or abandon it. Captain Jack persuades the other two that the gases in the moose will provide enough buoyancy to keep their canoe afloat. In a passage redolent of Faulkner among the Snopses, Dexter lets the trio get halfway across before the carcass vents a chilling sigh and the boat founders.

The consequences of the incident, however, are not so funny. Charley, who can't swim, nearly drowns. Outraged, he absents himself from Bill for a while, on a visit to his wife in Colorado. His huff keeps him from attending Bill when he is gunned down in Deadwood by Jack McCall, a nondescript whose only distinguishing feature up to that point is the uncanny devotion he inspires in cats.

WILD BILL doesn't make it even to the midpoint of Deadwood: during the rest of this hilarious and rousing novel, the other characters cope with his transformation from living to dead legend. Minor players come to the fore: Ci-an, the exquisite prostitute known as the China Doll; Boone, a cool and enterprising fellow who keeps a severed human head in a leather bag because if he ever gets to Laramie he can redeem it for $200; the Bottle Fiend, a "soft-brain" who runs the local bathhouse and has amassed a collection of over a thousand bottles. All of them become threads in the tapestry of Deadwood, and the town itself becomes the protagonist, much like Macondo in Garcia Marquez's One Hundred Years of Solitude.

Most affected (in both senses of the word) by Bill's death is Calamity Jane, whom Dexter portrays as a Dickensian fraud. Extraordinarily homely, a dipsomaniacal tomboy, Jane puts Bill's death to use in solidifying her claim to have married him. "You wasn't with him every minute of his life," she replies when Charley challenges her at Bill's graveside. "You wasn't even there when he got kilt, so how do you know what he done?" "The same way I know a horse never climbed a tree," says Charley.

Deadwood has its somber passages, as when a jealous john slays the China Doll, and its sedate characters, like Agnes Lake, Bill's true wife, who manages to appease Jane when they finally meet. But for the most part it's a carnal comedy about the way legends accrue. The kindest acts the townspeople perform for one another are not individual mercies but contributions to the collective celebration of Deadwood: the conferral of monikers like China Doll and Bottle Fiend, the tolerance of eccentricity, the trading and enhancing of anecdotes, the cooperative satisfaction of their common need: to be known for something. With its stylish humor and convincing demonstration of how the fables of the Wild West originated, Deadwood may well be the best Western ever written.