American Mayweather, 40, posted a video on Twitter confirming the fight details with the message: "It's official."
Irish UFC lightweight champion McGregor, 28, said: "The fight is on."
It is reported each fighter could earn as much as $100m (£78.4m) from the bout, which would be one of the richest in boxing history.
Mayweather, a former five-weight world champion and widely considered the best boxer of his generation, retired unbeaten in 2015 after 49 bouts.
That followed a successful defence of his WBC and WBA welterweight titles, a victory that equalled Rocky Marciano's career record of 49-0.
Mayweather retired for the first time in 2008 after 39 fights.
McGregor, who has never boxed professionally, became the UFC's first dual-division champion in November 2016 and has previously challenged Mayweather to a fight under mixed martial arts rules.
His boxing licence was granted by the California State Athletic Commission in December, allowing him to box in the US state.
UFC president Dana White, who took part in negotiations on behalf of McGregor, told ESPN: "We have been in talks for a while.
"They went smoothly. Floyd Mayweather surrounds himself with smart people and we got the deal done. The impossible deal is now done.
"The reason Conor McGregor's as big as he is is he'll fight anybody, anywhere, any time. He will go after Floyd Mayweather and he will try to knock him out."
The biggest pay-per-view fight ever?
White said the fight would take place at the T-Mobile Arena in Las Vegas and would be at 154lbs - light-middleweight, one category up from Mayweather's last bout in September 2015, when he gained a wide points win over compatriot Andre Berto.
When Mayweather defeated Filipino Manny Pacquiao on points in May 2015, the fight set a new American pay-per-view record of 4.6 million buys, and White expects that figure to be beaten.
"It's definitely going to be the biggest fight ever in combat sports history and probably going to be the biggest pay day ever - all sides involved are pretty happy with their deals," he said.
Leonard Ellerbe, chief executive of Mayweather Promotions, added: "There is a different feel from the Pacquiao fight. It is the unknown factor.
"There is not one place I go to with Floyd where he doesn't get asked the question: 'Floyd, are you going to fight Conor McGregor?' All Floyd thinks about is fighting Conor McGregor."
McGregor has won 21 and lost three of his 24 MMA contests, and White said his style would cause problems for Mayweather.
"Floyd Mayweather is 40 years old, he's always had problems with southpaws," added White.
"Conor McGregor is 28 and he is a southpaw. Whenever Conor hits people, they fall. He is 100% positive that he wins this fight, and I stopped doubting him a long time ago."