Late in the spring of 1876 Bat returned to Dodge City. The town was divided politically into two groups. One favored taming the rough element and the other championed a wide-open town. Buffalo had been replaced by cattle. Ranchers raising cattle in Texas were driving their herds to the railroad in Dodge for shipment to market in the East. Bat became a member of the "Dodge City Gang" - the element that favored a wide open town. The law and order group hired Wyatt Earp away from Wichita, Kansas, to serve as Deputy Marshal of Dodge under Marshal Larry Deger. Earp served as Deger's enforcer with the authority to hire additional deputies. Wyatt hired Bat and the two friends managed to keep the peace in Dodge largely without resorting to their firearms. In July 1876, Bat resigned and left Dodge to try his luck prospecting for gold in the Black Hills.
Bat did not prospect in the mountains of the Black Hills, instead he sought gold by gambling in Cheyenne, Wyoming. He did well and decided to return to Dodge City. In the fall of 1876 he once again went buffalo hunting, but in April 1877 he was actively participating in Dodge City politics and together with his older brother, Ed, was once again on the police force under Marshal Deger. In July 1877, Bat was appointed under-sheriff of Ford County under Sheriff Charlie Bassett. During this period Bat bought into the Lone Star Dance Hall. In October 1877 he announced that he was a candidate for sheriff of Ford County and sold his interest in the Lone Star. He was elected on November 6, 1877, narrowly defeating his old boss, Larry Deger by three votes. On April 9, 1878, then City Marshall, Ed Masterson, was shot and killed by Alf Walker. Rumor quickly circulated that Bat avenged his brother's death by personally killing Ed's murderer, but there is no evidence that this happened.
On May 8, 1878, Wyatt Earp returned to Dodge from the Black Hills gold country and was appointed deputy marshall under Charlie Bassett who had replaced Ed Masterson as City Marshall. That year it was estimated that 1,300 cowboys drove 265,000 longhorns to Dodge. Bat and Wyatt again coordinated their efforts to keep the peace without undue reliance on their guns. Dodge was definitely a rowdy town, but during the two years that Bat was sheriff of Ford County there were only seven homicides recorded and he only fired his weapon once in making an arrest. In the Eastern press, however, Dodge was rapidly becoming the Gomorrah of the Plains and Bat Masterson was gaining a reputation as a fierce killer. In the spring of 1879 the Santa Fe and the Denver & Rio Grande Railroads were engaged in the "Royal Gorge War" in Colorado. Bat organized a posse that included some known gun-fighters, including Doc Holliday, and went to the support of the Santa Fe. Although several skirmishes were fought, the dispute was settled in the courts without bloodshed.
Masterson was defeated in his bid for reelection in November 1879 and replaced as Sheriff of Ford County by George Hinkle on January 12, 1880. After giving up his position as sheriff, Bat left Dodge for Leadville, Colorado, where he did well at the gambling tables. After attending Republican Party meetings in Dodge City and Topeka, Kansas, Masterson, at the behest of Ben Thompson, went to Ogalla, Nebraska, to rescue Billy Thompson from a threatened lynching. Buffalo Bill Cody aided Masterson in this endeavor. After delivering Billy Thompson to his brother in Dodge, Bat went on to Kansas City where he held his own with some of the best known gamblers in the profession for six months. In February 1881, at Wyatt Earp's request, he took up a position as a faro dealer in the Oriental Saloon in Tombstone, Arizona. Earp was a partner in the business and needed Bat's professional assistance in the saloon as well as his gun in the fight that was brewing between the Earp brothers and a group of cowboys dedicated to driving the Earp brothers out of Tombstone.
Bat did participate in one posse action led by Wyatt Earp, but in April 1881 he received a mysterious unsigned telegram that implied that his brother Jim was in grave danger. Jim Masterson had been named City Marshall of Dodge City in November 1879. Jim's political patron was Mayor Dog Kelley. Kelley was defeated in the city council elections of April 1881 and Jim was fired as City Marshall by the new mayor, A.B. Webster. Tensions were high between the various factions when Bat arrived in town on April 16. The "Battle of the Plaza" erupted upon his arrival and several men were shot. Although who did what to whom was unclear, both Jim and Bat were forced to leave town. After leaving Dodge, Bat toured the Colorado mining camps and prospered as a professional gambler. Early in 1882 he went to New Orleans to watch the prize fight between Paddy Ryan and John L. Sullivan. After leaving New Orleans Bat went to Trinidad, Colorado, where the newly elected mayor appointed him city marshall.
On May 15, 1881, Doc Holiday was arrested in Denver on charges brought against him in Arizona. Wyatt Earp feared for Holiday's life if he was returned to Tombstone and asked Bat to help get him released. Sheriff Masterson appealed to Governor Frederick W. Pitkin and succeeded in gaining custody of Holiday after which Wyatt's friend was released. In 1883 the law was changed and the position of Sheriff of Trinidad was no longer appointed by the mayor. On April 3, Bat was defeated in his bid for election and was replaced by Louis M. Kreeger. During the campaigning. Kreeger's supporters made much of the fact that Bat had been engaged in gambling during the period that he had been sheriff. Shortly after leaving Trinidad Bat was briefly involved in an affair that the newspapers of the time referred to as the Dodge City War. It was but one more episode in the continuing struggle between opposing groups for the control of the saloon trade. Enlisting Wyatt Earp and a number of other well known frontiersmen, Bat successfully backed his friend, Luke Short, in his confrontation with the then dominant A.B. Webster faction in power.