The Mets pulled off the coup of this winter late Friday night. After a whirlwind 24 hours of negotiations, they agreed on a three-year contract with Yoenis Cespedes worth $75 million, according to a source — and even secured the one-year opt-out they wanted all along. The team has yet to announce the signing, pending a physical.

Wary of making too big a long-term commitment, the Mets also got the opt-out they preferred, which could work out to Cespedes’ advantage as well. For the first year, he will earn $27.5 million — the second-highest average annual value ever for a position player — and then can choose to become a free agent after the 2016 season, which would put him arguably at the top of a weak class that winter. The Mets also gave Cespedes a full no-trade clause for the duration of the contract.

Getting Cespedes, on their terms, turns out to be a major victory for the Mets, who had come under fire lately for not pursuing him more aggressively in recent weeks. That heat only intensified during the previous 24 hours when it was revealed that the rival Nationals had a five-year, $100-million offer on the table to Cespedes, and Washington appeared to be his only suitor.

VoteMets 2017: Keep 'em or dump 'em?

But Cespedes was willing to take less guaranteed money to stay in Flushing — another encouraging sign for the defending National League champs — and preferred the fit with the Mets, according to a person familiar with his thinking. Not only does he greatly improve the Mets’ chances of repeating by bolstering the lineup, as he did after the trade deadline last season, but the Mets were able to keep him away from their biggest threat in the NL East — a month after Daniel Murphy signed with the Nationals.

When the offseason began, the Mets figured they had little chance of retaining Cespedes, who previously had said he would be seeking a six-year deal worth somewhere in the neighborhood of $150 million. But as the Mets kept their distance, that market never developed for Cespedes, and his price dropped closer to the range Sandy Alderson was hoping for.

Basically, Alderson played these negotiations perfectly, capitalizing on Cespedes’ desire to return to the Mets as well as the ability to test the market again after one season. In the end, both sides apparently got what they wanted.

advertisement | advertise on newsday

The Mets not only held on to the No. 4 hitter they badly needed but went a long way toward showing that last season’s march to the World Series was no fluke, no one-and-done effort.

The Mets sacrificed a top-level pitching prospect, Michael Fulmer, to acquire Cespedes minutes before the July 31 non-waiver trade deadline, and he immediately proved to be worth the rental. Cespedes batted .287 with 17 home runs, 44 RBIs and a .942 OPS in 57 regular-season games after coming to the Mets. His presence alone certainly had a positive effect on the Mets’ offense, which went from dead last in the NL in OPS (.660) before the All-Star break to first (.770) in the second half.

The Cespedes deal, which is a manageable investment in the Mets’ eyes, will push their 2016 payroll to roughly $140 million, the highest it’s been since 2011, after a very active winter that included the additions of Neil Walker, Asdrubal Cabrera, Antonio Bastardo and Alejandro De Aza.

Mets videos

The Cespedes situation had kept Mets fans on the edge of their social-media seats. Until Friday, it appeared the Mets had very little chance of keeping him because of their ironclad stance that they did not want to give him more than a three-year deal.

Ultimately they didn’t — and still got him anyway.

Bastardo in, Torres out. The Mets completed another piece of business Friday when they announced the signing of lefthanded reliever Bastardo to a two-year, $12-million contract.

Bastardo, who should team with righthander Addison Reed to set up closer Jeurys Familia, went 4-1 with a 2.98 ERA and one save for the Pirates in 2015.

To make room on the roster, the Mets designated righthander Carlos Torres for assignment. A week ago, Torres agreed to a one-year, $1.05-million contract. Torres went 5-6 with a 4.68 ERA last season.

advertisement | advertise on newsday

With Anthony Rieber and Marc Carig