Schwartzman, the 24th seed from Argentina, won an opening set tie-break 7-5 against Berrettini of Italy on Court 18.
But the world no. 20 bounced back to win the second set, also decided on a tie-break (7-2) to level the match.
Schwartzman, who had beaten Peter Ebden and Dominik Koepfer earlier in the competition, responded by breaking serve in the third set, which he took 6-4. However, Berrettini, seeded at No. 17, continued to battle and he took the match into a deciding fifth set after taking the fourth set 7-5 on yet another tie-break, after saving three match points.
And it was Berrettini who managed to raise his game in the final set of a titanic battle, winning 6-3 to reach the fourth round of a grand slam for the first time and set up a first-ever meeting against eight-time champion Roger Federer.