The Pentagon awarded Microsoft Corp. a hotly contested and controversial contract to build a large cloud-computing system for the U.S. military in a major setback to rival Amazon.com Inc., which was widely seen as the front-runner.

The so-called JEDI contract could be worth up to $10 billion to Microsoft over a 10-year period if the Pentagon exercises all options under the deal, the Defense Department said late Friday.

Amazon was the only other remaining bidder after several other firms were earlier eliminated from contention.

The process for developing and awarding the Joint Enterprise Defense Infrastructure contract has been fraught, amid multiple conflict-of-interest allegations and legal challenges, as well as concerns that opting for a single-source vendor might not be best for the military. Defense Secretary Mark Esper announced his own top-to-bottom review in early August, after President Trump voiced concerns about JEDI and Amazon—a company he has frequently criticized during his presidency.

Mr. Esper this week said he was withdrawing from reviewing the contract to avoid the appearance of a conflict of interest. His son worked for one of the original bidders that was no longer in the running for the deal.