Borussia Dortmund clinched a top-four Bundesliga finish and a return to the Champions League next season with a 3-1 win at Mainz.