The contract can increase to $48 million in value through incentives.
Matz joined the Mets as a second-round pick in the 2009 MLB Draft. He made his MLB debut in 2015. He owns a 45-48 record with a 4.24 ERA in 141 career appearances.
He is set to join a Cardinals rotation that also features Adam Wainwright, Jack Flaherty, Dakota Hudson and Miles Mikolas.
Cardinals starting pitchers posted a 4.01 ERA last season, the 11th-best in baseball and sixth-best in the National League. They also tied for allowing the third-fewest home runs in MLB.