Businessman Anil Aggarwal, backed by the BJP, beat the Bahujan Samaj Party (BSP)'s Bhim Rao Ambedkar in a thriller that pitted Mayawati against the formidable strategic skills of BJP chief Amit Shah, who not just worked on sulking allies but also ensured that his party lawmakers went through several rounds of practice voting.
The BJP now has nine more Rajya Sabha seats from UP and Akhilesh Yadav's Samajwadi Party has taken one seat for its candidate Jaya Bachchan.
BSP leader Satish Mishra insisted the party was "satisfied" with the SP's efforts. "The SP and Congress transferred votes perfectly to us," Mr Mishra told reporters.
The BJP couldn't resist a dig. "The wise must learn from the bump and not fall in a ditch," said Chief Minister Yogi Adityanath, who had lost his home-base Gorakhpur to an SP-BSP ambush in the bypolls.
Soon after voting started, Mayawati was one down; her party's Anil Kumar Singh declared that he had cross-voted for the BJP. "I am with Maharaj-ji (Yogi Adityanath)," he said. Then Samajwadi lawmaker Nitin Agrawal cross-voted for the BJP; his father Naresh Agarwal had recently switched from the SP to BJP after being passed over for Jaya Bachchan.
With 18 lawmakers, Mayawati's candidate needed the first preference votes of 37 to win a Rajya Sabha seat. Bhim Ram Ambedkar won 32 first preference votes and his rival Anil Aggarwal won 22. With none making it to 37, it fell onto second preference votes, in which the BSP was outnumbered.
Mayawati's defeat also had a touch of karma.
One of the two independent lawmakers whose votes could have helped her is Raja Bhaiyya, who was jailed by Mayawati in the past. He said he would back the SP - not BSP - and also met with Chief Minister Yogi Adityanath after voting. The other independent member is Raja Bhaiyya's aide Vinod Saroj, who voted the BJP.
A third independent lawmaker, Aman Mani Tripathi, was sidelined by the SP; he too voted for the BJP candidate and called Yogi Adityanath his mentor.
A possibility that was briefly talked about was Akhilesh Yadav sacrificing his nominee Jaya Bachchan and getting his lawmakers to send Mayawati's candidate to the Rajya Sabha.
The BJP had 28 votes left over after its guaranteed eight seats. It needed nine votes to aim for its ninth seat and avenge its humiliation by the Mayawati-Akhilesh combine. Its ally Suheldev Bharatiya Samaj Party (SBSP) - upset with Yogi Adityanath - pledged the support of its four lawmakers after being placated by Amit Shah. The Apna Dal claimed that its nine legislators had voted for the BJP.