The team announced the move Monday, noting the Pirates will receive minor league pitcher Cody Ponce from Milwaukee.
The Athletic's Ken Rosenthal first reported the Brewers were trading for Lyles.
The Brewers badly needed to make a move prior to the July 31 trade deadline because their rotation has been decimated by injuries.
Brandon Woodruff, Jhoulys Chacin, Corbin Burnes and Jimmy Nelson are currently on the injured list. Woodruff, who was named to the National League All-Star team earlier this month, is expected to miss six weeks due to an oblique strain suffered during a July 21 start against the Arizona Diamondbacks.
Starting pitching has been an Achilles' heel for Milwaukee, even when everyone has been healthy. The defending NL Central champs rank 20th in MLB with a 4.84 ERA from their starters and 22nd with 533.2 innings pitched.
Lyles returns to the Brewers after posting a 3.31 ERA in 11 appearances for them last season. He has struggled in 2019 with a 5.36 ERA in 82.1 innings, but Milwaukee is at a point where it needs anyone who can eat innings to get it through the stretch run.
Ponce was a second-round by the Brewers in 2015. The 25-year-old has a 3.29 ERA and 44 strikeouts in 38.1 innings out of the bullpen in Double-A this season.