Walmart has agreed to buy India's leading online retailer Flipkart, paying $16 billion for a controlling stake of 77%.

Flipkart gives Walmart access to a market it has been trying to crack for years. The Arkansas-based retailer has been restricted by local regulations to operating wholesale outlets in the world's fastest growing economy.

"India is one of the most attractive retail markets in the world, given its size and growth rate, and our investment is an opportunity to partner with the company that is leading transformation of e-commerce in the market," Walmart CEO Doug McMillon said in a statement.

Walmart (WMT) will pump $2 billion in fresh investment into Flipkart as part of the deal. It could also bring in "additional potential investors" while retaining a majority stake, chief financial officer Brett Biggs said on a conference call.

Flipkart's strong presence in sectors such as fashion, electronics and digital payments were major factors behind Walmart's decision to buy the company, McMillon said.

News of the deal, which is still subject to regulatory approval, was let slip earlier Wednesday by SoftBank (SFTBF) CEO Masayoshi Son during an earnings presentation. The Japanese technology group invested $2.5 billion in Bangalore-based Flipkart last year. Son said the sale to Walmart valued SoftBank's stake at $4 billion -- implying a bumper return of 60%.

Many of Flipkart's other investors will retain stakes in the company, including co-founder Binny Bansal, Chinese tech giant Tencent (TCEHY) and Microsoft (MSFT).

Related: Walmart tightens its opioid prescription policies

Taking the fight to Amazon

Walmart's interest in investing in Flipkart was first reported in 2016, and the deal boosts its firepower in a battle with Amazon (AMZN) in a country of 1.3 billion people.

Amazon CEO Jeff Bezos has committed to investing at least $5 billion in his company's India business, saying in 2016 that he saw "huge potential" in the country. Amazon has introduced its Prime video and music services in India in its effort to challenge Flipkart, as well as its annual Prime Day blockbuster sale.

Flipkart was founded in 2007 by two former Amazon employees -- Sachin Bansal and Binny Bansal. (They are not related.) In a Facebook post, Flipkart executive chairman Sachin Bansal confirmed that he's leaving the company after 10 years. Neither Walmart nor Flipkart would say how large Sachin Bansal's stake was, or to whom he sold it.

Flipkart now has an even bigger war chest to fend off Amazon in an e-commerce market that Morgan Stanley estimates will be worth $200 billion by 2026.

The Indian firm could also speed up its expansion into groceries, an area that has already pitted Amazon against Walmart in the United States.

Walmart cited estimates that online shopping in India will grow by 36% per year over the next five years. That's four times faster than the country's traditional retail market.

Walmart has been selling assets elsewhere in preparation for its big move in India. The company recently offloaded its UK supermarket business to Sainsbury's (JSAIY) for $4.1 billion and a 42% stake in the combined company.

"This is clearly an investment for the future, and... is indicative of Walmart's long standing strategy of shifting resources into higher growth potential markets," said Charlie O'Shea, lead retail analyst at Moody's.

Amazon may have been trying to spoil Walmart's plans until the very last minute. Indian media reported in early May that it made a counteroffer to buy Flipkart.

-- Matt Egan contributed to this article.