Murray was made to work hard throughout and the tone for a tough match on the Louis Armstrong Court was set early on, with the Argentine using a fierce forehand to stay competitive.
Late in the first set Murray made a breakthrough as Mayer tired slightly, taking advantage of a slack drop shot to claim the opener 7-5.
The Scot then wasted little time in extending his advantage, earning successive breaks as Mayer started to make unforced errors with an aggressive, all-action style.
Mayer refused to be downbeat as he slipped two sets behind, with Murray bungling a smash as he quickly slipped 3-0 down in the third.
The Argentine pressed home his advantage with variety and a succession of backhand winners to reduce the deficit, taking the third set 6-3.
Murray regained his composure in the fourth set and looked desperate to get the match wrapped up, racing through successive breaks to finally break Mayer's resistance.
After two hours and 44 minutes Murray eventually got the job done, prevailing 7-5 6-1 3-6 6-1 to book a third-round meeting with Florian Mayer on Sunday.