Gomes, 22, joins after helping Portugal win Euro 2016, and could cost a further 20m euros (£16.7m) in add-on fees.
"Barca relates to my way of playing football and my identity," said Gomes.
Mascherano, 32, has made 247 appearances for Barcelona since joining from Liverpool in 2010, winning four La Liga titles and two Champions Leagues.
Gomes is the club's fourth summer signing, after French defenders Samuel Umtiti and Lucas Digne joined from Lyon and Paris St-Germain respectively, and Denis Suarez re-signed after a season at Villarreal.
Barcelona have signed midfielder Andre Gomes from Valencia for an initial 35m euros (£29.3m), and given Javier Mascherano a new three-year contract.