The internal combustion engine was dealt another blow on Wednesday when the U.K. pledged to ban the sale of new gasoline and diesel cars by 2040, following a similar move three weeks ago by France.

While far from a knockout, the decisions by the U.K. and France--which together account for about a third of new cars sold in the European Union--are among the most aggressive moves by governments recently to legislate away the traditional gasoline- or diesel-burning engine, which has been for more than 100 years been the preferred method of powering passenger vehicles.

For decades, governments have struggled to rein in the pollutants the engines caused but with few alternatives have balked at banning them outright.

Volvo's recent announcement that it would only sell fully electric or hybrid cars starting in 2019 and the arrival of Tesla Inc.'s $35,000 Model 3 have put further pressure on gasoline and diesel engines.

"We can't carry on with diesel and petrol cars, not just because of the health problems, but also because the emissions they cause will accelerate climate change," U.K. Environment Secretary Michael Gove said in an interview with the British Broadcasting Corp.

The government later Wednesday will announce a package of more than GBP200 million ($260 million) that will enable local authorities to draw up plans--including placing restrictions on drivers--to target roads and areas with high pollution levels, Mr. Gove said.

The tide has been swelling for some time against the internal combustion engine, but there have been long-term promises by governments for decades to move away from fossil-fuel powered transportation, and there are plenty of reasons why the U.K. and France could fall short.

Car companies and their suppliers employ hundreds of thousands of people in both countries in relatively well-paid jobs, many of which can be obtained without a college education. The manufacturers are likely to lobby against what they have argued is a premature move to ban gasoline and diesel engines. The lobby representing the U.K. car industry has already said a ban could cripple the sector and cost jobs.

None of the major car makers made immediate statements in the wake of the U.K. government's announcement.

Much of the future of the internal combustion engine will depend on what happens in China and the U.S., the world's two largest car markets. China has heavily incentivized electric cars in recent years and has set aggressive targets for their penetration, but has stopped short of discussing a ban on fossil-fuel powered vehicles. Despite the incentives, electric cars still make up less than 1% of the country's fleet of vehicles.

In the U.S., low gasoline prices in recent years have diminished the appeal of electric cars as has their cost. Nevertheless, car makers build and market electric cars as part of their effort to meet emissions standards.

Norway, where almost a third of the passenger cars sold last year were electric or plug-in hybrids, has taken the most aggressive steps in Europe to phase out the internal combustion engine. The country has set a goal to by 2025 to have only new cars that are either electric- or hydrogen-powered or are plug-in hybrids that can run on a gasoline engine when their electric power runs out.

But Norway's success hasn't been replicated in the rest of Europe, where only 1% of cars sold in 2016 were electric or plug-in hybrids

Europe's large cities have long struggled to keep pollution under control and some have recently taken matters into their own hands by banning or restricting diesel vehicles, which have come under the spotlight following the Volkswagen AG emissions scandal.

London is facing record levels of pollution, in part because of emissions from diesel and gasoline cars. Around 23,500 deaths in Britain every year are linked to pollution, according to the Department for Environment, Food and Rural Affairs.

The U.K. got a boost to its green credentials with the announcement this week that BMW AG will assemble its all-electric Mini in the U.K. beginning in 2019.

Jenny Gross in London contributed to this article.

Write to Eric Sylvers at eric.sylvers@wsj.com