HOUSTON — Voters here on Saturday kept the largest city in Texas in Democratic hands, electing a longtime lawmaker as mayor in a tight race over a Republican-leaning businessman.

The lawmaker, State Representative Sylvester Turner, defeated his rival, Bill King, as rainy weather appeared to have lessened turnout in the runoff election.

The vote represented a political crossroads of sorts for the country’s fourth-most populous city. For nearly six years, Houston has been led by Mayor Annise D. Parker, a Democrat who became one of the first openly gay mayors in America after winning office in December 2009. Houston has long been more diverse and Democratic-leaning than most other parts of Texas, and although officially the mayor’s seat is nonpartisan, it has been nearly 40 years since a Republican was elected mayor. (The last one was Jim McConn, who served from 1978 to 1982.)

But as Ms. Parker’s third and final term neared an end, the city was caught up in pragmatic concerns and complaints, from pothole-ridden streets to the costly pension system for public employees, a signal to many that Democrats’ lock on the office was in jeopardy.