Bear in mind that the last Kettler duke of Courland died without children in 1737. And the wife of the previous duke of Courland was the same woman who was now empress of Russia (Anna Ivanovna). There had been various plans for a second marriage for her to Maurice, Comte de Saxe and someone else can't remember whom, with the deal that in exchange for the marriage they would become duke of Courland.
There was even a plan under Empress Anne to transfer the duchy to Prince Ludwig Ernst of Brunswick on his marriage to Grand Duchess Elizabeth Petrovna (later Empress Elizabeth), in exchange for Elizabeth renouncing her rights to the Imperial crown to Anne's niece and Ludwig Ernst's sister-in-law, Anna Leopoldovna (born Elisabeth Catharina Christina of Mecklenburg-Schwerin, and married to Anton Ulrich of Brunswick, brother of Ludwig Ernst).
Elizabeth refused to renounce her rights, and Ludwig was sent home to Brunswick.
Anna then appointed her favorite - and reportedly, her lover - Ernst von Biren (who changed the spelling to the French 'Biron') as duke of Courland. However, Ernst was thrown in jail shortly after Anna's death, and Prince Charles of Saxony was appointed duke. Unfortunately, him being Catholic rubbed the mostly Lutheran populace up the wrong way. When in July 1762 the Tsarina
Catherine the Great —who had been unapproving of Duke Karl on the basis of his lacking interest in the mental education of his subjects— took the Russian throne after a coup d'état. She allowed the now entirely rehabilitated Biron to return from his exile and exercised a substantial diplomatic pressure on Saxony with the purpose of restoring him to his old office as Duke. And it was Ernst's son, Peter, Duke of Courland, who
sold Courland to Catherine the Great or Paul I for a sizeable pension and a life in St. Petersburg.