HOUSTON—An energized Democratic electorate wasn’t enough to defeat Texas Sen. Ted Cruz this month, but in the state’s most populous county, it created a blue landslide.

Nearly every Republican in Harris County, home to Houston, was unseated on Nov. 6, including 59 judges and the top executive, Ed Emmett, a moderate who won in 2014 with 83% of the vote.

Mr. Emmett, whose official title is county judge and who was widely lauded for steering the county through the devastation of Hurricane Harvey last year, lost to political novice Lina Hidalgo, a Democrat who said she has never previously attended a meeting of the Harris County Commissioners Court, which she will soon oversee. The 27-year-old won by less than 2 percentage points and will now help run a metro area that encompasses some 4.6 million people, the third-most-populous county in the U.S.

The Harris County election drew national attention in part because 17 African-American women, all Democrats, won judgeships, a symbolic shift for one of the nation’s most diverse cities. But it also demonstrates the growing political divide between more liberal cities and conservative rural areas that is evident even in red states like Texas, where every elected statewide official is a Republican.

Political analysts say some votes for Ms. Hidalgo likely came not from people who specifically preferred her over Mr. Emmett, but rather Democrats who turned out to vote for Senate candidate Beto O’Rourke and used a “straight-ticket” option to select Democrats in every race with the click of a single button. Mr. O’Rourke lost to Mr. Cruz statewide by 50.9% to 48.3%, but won Harris County with nearly 58% of votes cast.