Amazon.com Inc. abandoned its $2.5 billion plan to build a New York City headquarters, undoing one of the country’s biggest economic-development deals because it said it was troubled by growing political opposition to subsidies to one of the world’s richest companies.

The move reverses Amazon’s very public, yearlong sweepstakes that sparked bids from more than 200 locales around the country and left Northern Virginia and New York City the winners.

The...