The 27-year-old initially moved to Turin on loan for six million euros last summer with a 40 million euro option to buy.
And the Brazilian was one of the key players for Massimiliano Allegri as the club wrapped up a seventh straight Serie A title and fourth Italian Cup in a row.
“Juventus have exercised the option for the definitive acquisition of the registration rights of Douglas Costa from Bayern Munich for €40 million to be paid in two financial years,” the club said in a statement.
“Juventus and the player signed a contract of employment until 30 June 2022.”
Juventus added that the German champions could be paid a further fee of one million euros depending on Costa’s future performances.
Costa scored six goals and contributed 14 assists in 47 competitive games during his first season in Italy.
He completed the highest number of dribbles in Serie A, with 106.
Costa joined Bayern from Shakhtar Donetsk for 30 million euros in 2015, but struggled to follow up on his strong debut season in the 2016/17 campaign before moving to Italy.
Download our app and read this and other great stories on the move. Available for Android and iOS.