The U.S. trade deficit widened sharply in October as exports weakened following a summer surge, and imports jumped, setting up a likely drag on overall economic growth in the final months of 2016.

The trade gap for goods and services surged 17.8% from a month earlier to a seasonally adjusted $42.6 billion in October, the Commerce Department said Tuesday. That was the steepest one-month rise since March 2015, and took the deficit to its highest level since June. Economists had expected an October trade gap of $42.1 billion.

Looking ahead, “I would expect the trade deficit to widen next year, as solid domestic demand will boost imports and a renewed appreciation in the dollar will limit the strength in exports,” Amherst Pierpont Securities chief economist Stephen Stanley said in a note to clients.

Exports fell 1.8% from September, the largest drop since January, and imports rose 1.3% in October. The fall in exports included declining shipments of soybeans, corn and consumer goods while the import rise included stronger domestic demand for foreign-made pharmaceuticals, cellphones and capital goods.

On a non-seasonally adjusted basis, imports from China and Mexico hit their highest levels in a year. Exports to China were the most since December 2013 and exports to Japan were at the highest level since August 2014.

The value of petroleum imports, seasonally adjusted, was the highest in 13 months as oil prices marched higher. On a non-seasonally adjusted basis, the U.S. in October imported a daily average of 7.3 million barrels of crude oil, down from 7.9 million a day in September but at a higher per-unit price of $40.01 in October versus $39.02 in September.

A surge in soybean exports during the third quarter helped narrow the overall trade gap and offered a tailwind for overall economic growth. Net exports contributed 0.87 percentage point to the 3.2% seasonally adjusted annual growth rate for gross domestic product in the July-to-September period, according to Commerce Department data. It was the third consecutive quarter that net exports were a positive contributor to GDP growth.

Economists had expected the soybean-fueled export boost to be short-lived, however, and October’s increase in the trade deficit largely erased the gains of the prior three months. Overall economic growth could decelerate during the final three months of the year, partly because imports are a subtraction in the calculation of GDP.

Joshua Shapiro, chief U.S. economist at MFR Inc., said in a note to clients that it appeared net exports “will exert a significant downward influence” on fourth-quarter GDP growth. But he warned that “much can change with the release of November and December trade data, so this is still a highly tentative conclusion.” Trade data can be volatile from month to month. More broadly, U.S. trade with the rest of the world has slumped this year. Exports during the first 10 months of the year were down 3.1% from the same period in 2015, and imports declined 2.9% year-to-date, according to Tuesday’s report. The trade deficit narrowed 2.1% in the first 10 months of 2016 from a year earlier.

U.S. manufacturers and other exporters have been hampered in recent years by soft foreign demand and a strong dollar, which makes American-made products more expensive for overseas customers while depressing import prices. The dollar rose furtheragainst other major currencies following President-elect Donald Trump’s victory on Nov. 8, though Tuesday’s report reflected trade flows ahead of Election Day.

“We hope for better export performance over the next few months, as the lagged effect of the drop in the dollar in the early part of this year works through, but that boost will not persist because the dollar has risen strongly more recently,” Pantheon Macroeconomics chief economist Ian Shepherdson said in a note to clients.

During the campaign, Mr. Trump—who will take office in January—was sharply critical of free-trade deals that he said have cost the U.S. jobs, especially in the manufacturing sector. He said he would work to reduce the nation’s trade deficit as part of his plan to boost economic growth.

“We’re going to fight for every last American job,” Mr. Trump said last week during a rally in Cincinnati, Ohio. “It is time to remove the rust from the Rust Belt and usher in a new industrial revolution.”

Mr. Trump has been at odds with the Republican Party’s traditional support for free trade, which most economists say offers overall if uneven benefits including lower prices for consumers. Due to advances in productivity, U.S. manufacturing production has generally risen despite falling factory employment in recent decades.

The U.S. has run persistent trade deficits for decades by importing more goods than it exports, though the nation enjoys a trade surplus for services and the overall trade gap narrowed after the 2007-09 recession as domestic energy production reduced reliance on foreign oil.