Berrettini edged a thrilling first set 7-5, which clocked in at 75 minutes long, and faced another stern challenge in the second.
At one point trailing 4-2, Berrettini fought back to set-up a tie-breaker that was equally tight, with Carballes Baena fighting back from 4-2 down to force a decider.
With the clock past the three-hour mark, Carballes Baena capped off a remarkable comeback in the third set to send Berrettini tumbling out of the tournament, coming back from a break down once again.
Earlier in the day, fourth seed Maxime Cressy was eliminated by American compatriot J.J. Wolf after a 6-3 6-4 defeat and fifth seed Aslan Karatsev was also eliminated, losing 6-1 6-7(6-8) 6-1 in a two-and-a-half-hour showdown.
At the Gijon Open, second seed Pablo Carreno Busta sealed a spot in the quarterfinals with a 6-4 7-6(3) victory over Brazilian Thiago Monteiro, forcing three errors from Monteiro in the second-set tie-breaker.
Fifth seed Tommy Paul secured safe passage to the quarter-finals, beating Nicolas Alvarez Varona 6-3 7-6 (7-3), with Pedro Cachin and Sebastian Korda among the day's other winners.