Roger Federer won a thrilling three-hour contest against Nick Kyrgios to set up a final against Rafael Nadal at the Miami Open.

The Swiss, 35, won 7-6 (11-9) 6-7 (9-11) 7-6 (7-5) to take his 2017 record to 18 wins and one defeat.

Federer will take on Nadal for the 37th time on Sunday, and the second final this year after beating the Spaniard at January’s Australian Open.

Nadal, 30, beat Italy’s Fabio Fognini 6-1 7-5 in the first semi-final.

Fognini, 29, had become the first unseeded player in 10 years to make the last four but was no match for Nadal, who will try to win a first Miami title in his fifth final on Sunday.

“It’s great to be in the final for me,” said Nadal after winning the first of the semi-finals. “I am excited to play another final of an important event.

“If it’s Roger, it’s going to be another one for both of us, and that’s it. Just another one.”

Federer made sure that there will be another episode to his rivalry with Nadal, which began in Miami 13 years ago.

The 18-time Grand Slam champion maintained his spectacular run of results with victory over the in-form Kyrgios.

“It felt very good,” said the Swiss, who last won the Miami title in 2006. “You don’t very often play three breakers in a match.

“Winning breakers is always such a thrill. I tried to really fight for it. I can’t always show my fighting skills, it is great winning this way.”

Despite playing in front of a heavily pro-Federer crowd, Kyrgios extended the former champion over three hours and 10 minutes, with the 21-year-old Australian showing his frustration by smashing his racquet after losing match point.

“I had some ups and downs, bit of a rollercoaster,” said Kyrgios.

“Ultimately I think I put in a good performance. I thought the crowd would’ve enjoyed watching it, people at home would’ve enjoyed watching it. But I wouldn’t be surprised if they found something bad, though.”

On Saturday, British number one Johanna Konta plays Denmark’s Caroline Wozniacki in the women’s final at 18:00 BST – with live text commentary on the BBC Sport website.

Konta, 25, became the first British woman to reach the Miami Open final with a 6-4 7-5 victory over Venus Williams in the last four on Thursday.