The shock move, which is understood to have cost tens of millions of pounds, will take effect from next season.
It means BT – whose chief executive is Ian Livingstone, a non-executive director of Celtic – will take on the rights to show 30 SPL and 10 Rangers games in the SFL Third Division each season until 2017.
They will be shown as part of the new BT Sport TV package set to launch in July.
BT is also taking on all ESPN's other UK and Ireland TV channels, but ESPN Classic is not part of the BT deal.
BT's live sport will include English Premier League games and Aviva Premiership Rugby on channels BT Sport 1 and 2.
It will show the Scottish games, as well as FA Cup, Europa League, Bundesliga and Emirates Cup games on an ESPN-branded channel.
Customers will still able to subscribe through Sky or Virgin – as well as BT Vision.
But viewers have been assured nothing will change until the end of the current season.
A spokesman for ESPN said: "ESPN viewers will see no changes until the completion of the deal and regulatory approval, which could come at the end of July.
"Until then customers will have the same channel, the same coverage and the same service. Thereafter is a matter for BT."
At the start of next season viewers will have the option to sign up with BT.
Sky has four years left on a deal it signed last summer giving it the rights to show 30 SPL matches and five Rangers SFL matches per season.
SPL chief executive Neil Doncaster has called the acquisition "positive for the Scottish game".
He said: "It means top-flight football will continue to be broadcast by Sky Sports and now BT, providing a solid financial bedrock and the best possible platform to promote the game in Scotland."