When Patrick Byrne vacated the helm of Overstock last month, he left a nasty surprise for short sellers.

Brokers at JPMorgan and Morgan Stanley in recent days have been helping short sellers roll back a bizarre plan by Overstock’s controversial ex-CEO to pay shareholders their next dividend in the form of an obscure cryptocurrency, sources told The Post.

The crypto-dividend plan was devised by Byrne — who resigned Aug. 22 after claiming he was romantically entangled with a Russian spy — to thwart Overstock’s short sellers, with whom he has been tangling for decades. The plan worked — for a time anyway — and sent Overstock shares surging 60% over the last two weeks, to a 52-week high of $29.75 in midday trading Friday.

That’s because short sellers — who place complex trades that are effectively bets that a stock will drop — were concerned about getting stuck with the stock’s blockchain-based dividends, sources said. To protect themselves, they began to unwind their short positions ahead of the dividend hitting, which drove the stock higher.

“Byrne figured out how to stick it to Wall Street,” a source said. “He designed the dividend to create short covering.”

It’s not Byrne’s first bout with short sellers. In 2005, the eccentric CEO became infamous on Wall Street for his claims of a short-selling conspiracy against Overstock headed by an unidentified “Sith Lord” — a reference to “Star Wars” characters who draw on the dark side of the Force.

Byrne declined to identify the “Sith Lord” but described him as “a master criminal from the 1980s” and claimed regulators were investigating.

Under the dividend plan revealed July 30, a crypto-payment will be awarded to Overstock shareholders of record as of Sept. 23 on tZero, a blockchain-based trading platform operated by an affiliate of Overstock.

The dividends, accessible only through a Dinosaur Financial Group brokerage account, aren’t payable until Nov. 15 and cannot be traded for six months afterward — or May 15, at the earliest.

Byrne’s short squeeze has deflated in recent days, however — thanks to brokerage firms JPMorgan and Morgan Stanley agreeing to take cash of an equivalent value to the digital dividend when short sellers return their borrowed shares, sources said.

Overstock shares buckled as word of the brokerage concession began to spread early Friday because it meant short sellers could maintain their short positions, insiders said.

The furniture seller’s stock on Tuesday closed at $17.60, off more than 40% from Friday’s peak.

Overstock didn’t respond to requests for comment. Morgan Stanley and JPMorgan declined to comment.

Byrne resigned 10 days after he confirmed he’d been romantically involved with convicted Russian spy Maria Butina — a relationship he said led to his involvement in the FBI’s probes of Russia and Hillary Clinton.

“In fact, I am the notorious ‘missing Chapter 1’ of the Russian investigation,” Byrne cryptically declared on Aug. 12 — sending the stock down 36% over two days.