The Portugal international moves to Istanbul on a free transfer after a decade at the Santiago Bernabeu.
Racking up over 300 appearances for Los Blancos, the centre-back bows out having helped Madrid to a Liga and Champions League double in his final campaign.
Besiktas president Fikret Orman said that the Portugal international would add leadership to the club's backline as they aim to retain the Super Lig crown they won last season.
Orman added: "We have added a very important player with a career and personality that we like."
Pepe won three league titles, as many Champions League crowns and two Club World Cup medals at Madrid and has set his sights on more silverware at Besiktas: "My goal is to continue to win trophies.
"I love my job. I love to give my all on the pitch."