Ryan McMahon hit a two-run home run in the eighth inning to lift the New York Yankees to a 4-2 win against the Kansas City Royals.