Berdych, who only took a wild card here to get more clay matches with Roland Garros just three weeks away, almost found himself on the sidelines as he dropped the first set to his 88th-ranked Argentine opponent.
The sixth-ranked Czech survived a roller-coaster final set and will Saturday face Portugal's Gastao Elias or Romanian Victor Hanescu.
Raonic again disappointed on clay, bundled out in just under 90 minutes 7-5, 6-4 by Argentine Carlos Berlocq.
The Canadian was limited to six aces and the same number of double-faults in defeat as he lost serve four times.
"At the end of the day, I want to do much better this season on clay than I have in the past," he said. "I'm putting a lot of attention on Madrid (next week) and Paris.
"On clay, training doesn't mean a lot. You need confidence that only comes through wins."
Number 62 ranked Berlocq next plays 98th-ranked Daniel Gimeno-Traver, after the Spaniard upset third-seeded compatriot Marcel Granollers 6-1, 6-2.
Berdych was stunned in the opening set by Mayer after leading with a break.
But the world number six recovered his poise in the second set despite again going down a break.
In the third, it looked like the Czech was in charge after breaking for 2-0 only to lose it a game later to the challenger despite leading 40-0.
Berdych again dropped serve in the fifth game for 2-3 but then got it back to 4-4. The seed made a final stand and emerged with a nervous victory on his second match point after two hours, nine minutes with a concluding break.
In the women's event Svetlana Kuznetsova reached her first final in more than three years on Friday with a 6-3, 6-1 win over fellow Russian Elena Vesnina to set up a clash with Carla Suarez Navarro.
The Russian seventh-seed, who owns Grand Slam titles from 2004 in Paris and 2009 in New York, will take on the Spanish top seed competing in her third straight final here but without a career trophy of any kind.
Suarez Navarro overcame Romanian qualifier Irina Begu 7-6 (7/3), 6-1 in one and three-quarter hours.
Tomas Berdych Battles Through to Semis as Milos Raonic Exits in Portugal Open
Advertisement