The $7.5 million annually puts McKinnon as the fourth-highest paid running back in the NFL behind only Le'Veon Bell of the Pittsburgh Steelers, Devonta Freeman of the Atlanta Falcons and LeSean McCoy of the Buffalo Bills.
The deal will become official when the league's new year starts later on Wednesday.
The 25-year-old McKinnon recorded 1,918 yards rushing with seven touchdowns while adding 142 receptions for 984 yards and five scores in 58 games with the Vikings.
The need for McKinnon arose for the 49ers with fellow running back Carlos Hyde expected to agree to a three-year deal with the Cleveland Browns. Hyde, who played collegiately at Ohio State, rushed 240 times for 938 yards and eight touchdowns last season and also reeled in a career-high 59 receptions for 350 yards.