In return, Cleveland will acquire minor league outfielder James Ramsey from the Cards.
Masterson, a free agent after this season, is 4-6 with a 5.51 ERA this year for Cleveland. He's struggled with a knee injury and was placed on the 15-day disabled list July 8 after allowing five runs and six hits in two-plus innings against the New York Yankees. He's yet to be activated.
Prior to the DL stint, Masterson hadn't won since June 8.
The 29-year-old right-hander made the All-Star team last season when he went 14-10 with three shutouts. The Indians considered him to be their staff ace going into this season, naming him their Opening Day starter.
"I was surprised,'' Masterson said. "It's bittersweet. I'm sad to leave the fellas and the fight we're making here in Cleveland, but I'm excited to get to St. Louis and be a part of what they have going there.''
Masterson and his agent, Randy Rowley, discussed a contract extension with the Indians in spring training, but negotiations broke off with no resolution in late March. Masterson had reportedly been seeking a two- or three-year deal for about $17 million a year.
Masterson was acquired by the Indians at the trade deadline in 2009 in the deal that sent catcher Victor Martinez to the Red Sox. Before Tuesday's series opener against Seattle, Masterson said he wouldn't be surprised if the Indians made any moves but he was hoping to stay and help them make a playoff push.
Masterson went 48-61 with Cleveland.
Ramsey, 24, was the 23rd overall pick in the 2012 draft out of Florida State. He's a .266 hitter with a .368 on base percentage in 235 minor league games.
He has spent the entire 2014 season to date with Double-A Springfield, hitting .300 with 14 doubles, 13 home runs (sixth-most in Texas League) and 36 RBIs in 67 games.
He started in the Futures Game earlier this month.
Indians manager Terry Francona said the trade isn't a signal that his team is giving up.
"We will never raise the white flag,'' he said. "That's not an option. Anybody who stands next to me in the dugout knows that will never happen.''
Francona was Boston's manager when Masterson was dealt to Cleveland.
"The hug was a little longer than normal,'' Francona said. "We may part teams, but my admiration for him isn't parting.''
Joked Masterson: "Tito keeps trading me away. What's up with that?''
ESPN.com's Jerry Crasnick and The Associated Press contributed to this report.