Democratic Rep. Mike Honda of San Jose was narrowly behind attorney Ro Khanna Tuesday in the primary race for the South Bay congressional seat he has held for eight terms, setting up a rematch of their general election battle two years ago.

Honda and his Democratic challenger will square off in November as the top two finishers in the 17th Congressional District, which includes much of Silicon Valley, including Fremont, Milpitas, Newark, Santa Clara, Cupertino, Sunnyvale and part of San Jose.

With 100 percent of the precincts reporting, Khanna had 38.3 percent of the vote to Honda’s 38.1 percent.

Khanna, a Fremont attorney, holds a much stronger hand than he did in 2014. While Honda, who is seeking a ninth term in Congress, trampled Khanna, 48 percent to 28 percent, in the primary two years ago, Khanna slashed that margin dramatically Tuesday.

“With individuals ready to trample on the middle class bankrolling my opponent’s campaign, we know that this is going to be one of the closest congressional races in the country,” Honda said in a statement issued late Tuesday.

Despite Khanna’s strong showing, the contest is far from over. In 2014, Khanna rallied from a terrible primary bruising to lose by just four percentage points in the November election. Both men have said throughout the campaign that they expect the typically large Democratic turnout during a presidential election year to carry them to the top in the fall.

In a district where Democrats have a 44 percent to 19 percent registration edge over Republicans, the GOP contenders in the primary ran far down the track.

Khanna, 39, never really stopped running after his 2014 loss, announcing his new campaign just months after his defeat. But rather than presenting himself principally as the congressman from tech-heavy Silicon Valley, as he did during his unsuccessful challenge to Honda, Khanna this time concentrated more on local issues, showing up at city events to talk about regional problems.

Honda, 74, who had not faced a serious re-election threat before 2014, also upped his campaign game, spending more time in the district and telling voters nonstop about what he has done for the region in his years in public office.

It was a tough primary campaign that is likely to get nastier in the months leading to the November election. Khanna has slammed Honda continually on a House ethics investigation into allegations that the congressman’s staff broke rules by working too closely with his campaign team.

For his part, Honda has characterized Khanna as someone more interested in getting elected to Congress than in actually serving the community.

Elsewhere in the state, the vote counts were more certain, and the House races were little more than warm-ups for fall rematches. With the top two candidates in each contest, regardless of party, meeting again in November, the primary often becomes little more than a chance for the leading candidates to see where they stand and decide what needs to be done in the fall.

In the Seventh Congressional District, a GOP target that includes several Sacramento suburbs, two-term Democratic Rep. Ami Bera beat Republican Scott Jones, the Sacramento County sheriff, in a district Bera won by only a single percentage point two years ago.

In District 12, House Minority Leader Nancy Pelosi handily led the field, on track to face Republican Bob Miller in the fall.

The Democrats will be focusing their November efforts on a couple of Central Valley seats now held by Republicans.

In the 21st Congressional District, Republican Rep. David Valadao of Hanford (Kings County) led second-place Daniel Parra of Fowler (Fresno County).

In the 10th Congressional District, Rep. Jeff Denham of Turlock (Stanislaus County) led the field, and will face Democrat Michael Eggman of Modestoin the fall.

John Wildermuth and Rachel Swan are San Francisco Chronicle staff writers. Email: jwildermuth@sfchronicle.com, rswan@sfchronicle.com Twitter: jfwildermuth, @rachelswan