Juve can purchase Costa outright for 40 million euros ($45.7 million) if they trigger the option before July 2018.
Juve can purchase Costa outright for 40 million euros ($45.7 million) if they trigger the option before July 2018 and will pay Bayern an initial six million euros for the 26-year-old as part of the loan agreement.
Costa's departure comes a day after Bayern completed a deal for Colombia playmaker James Rodriguez from Real Madrid on a two-year loan.
Costa scored 14 goals and provided 27 assists in 77 appearances for Bayern, winning the Bundesliga twice in as many seasons after moving to Germany from Shakhtar Donetsk in 2015.
"I'd like to thank Douglas for his performances. After two successful years and four titles it was his big wish to take on a new challenge," said Bayern chairman Karl-Heinz Rummenigge.
"We granted his request, and we wish him and his family all the best for the future in Italy."