The 25-year-old joined Boro on loan in February and earned a permanent deal despite making only one appearance, in a 1-0 defeat at Sheffield Wednesday in March.
Mejias will battle it out with Jason Steele and Dimi Konstantopoulos for the No.1 jersey at the Riverside Stadium.
"It is great to have everything sorted out," Mejias told the club's official website.
"I know it is a big move from Real Madrid, but it is one I had to make and I'm very happy to have made it. I enjoyed myself here last season.
"I am competing with two very good goalkeepers in Dimi and Steeley. Nothing has changed and we are all in a fight for the spot, we all know that. All I know is I am delighted to be here and to be given a chance."