Ralph S. Northam, the current lieutenant governor, and Ed Gillespie, a former chairman of the Republican National Committee, won their respective primaries to replace Gov. Terry McAuliffe.

The Republican contest was surprisingly competitive. Corey Stewart, a former state chairman for the Trump campaign who campaigned on preserving Confederate monuments, narrowly lost to the heavily favored Mr. Gillespie. Mr. Northam defeated Tom Perriello, a former congressman who counted Senators Bernie Sanders and Elizabeth Warren as big-name backers.