The Vermont senator still trails Hillary Clinton in the overall contest for delegates but this win keeps his slim hopes alive.
“We are going to fight for every last vote,” he said in a victory speech that also attacked Republican Donald Trump.
Mr Trump was declared the winner in West Virginia and in Nebraska.
His last remaining rivals dropped out last week but remained on the ballot.
But Mr Trump faces a huge task in trying to get the Republican party behind him, as doubts persist about his substance and style.
House Speaker Paul Ryan, the party’s highest-ranking elected official, has said he is unable to endorse the New York businessman because he lacks conservative principles.
Anthony Zurcher, BBC News, Washington
With victories in Indiana and now West Virginia, Bernie Sanders has started another winning streak against Hillary Clinton. Like his five-state run in April, however, it will do little to slow her steady march to the Democratic nomination.
Exit polls show the West Virginia vote was particularly quirky. Almost 40% of Democratic voters there said they wanted a president less liberal than Barack Obama – and that group favoured avowed socialist Sanders by more than a two-to-one margin.
Mr Sanders also overwhelmingly carried the 27% who want a president more liberal than Mr Obama.
Such a result makes sense if some of Sanders’s support is, in fact, an anyone-but-Clinton vote.
At this point the Vermont senator will likely take help wherever he can get it. He should be competitive in the next four contests, but could hit a wall in the June mega-prize, California. Its diverse electorate favours Mrs Clinton, and anything but an unprecedented Sanders win there would seal his fate.
Until then, however, Mr Sanders will continue to be a constant and unpleasant reminder to Mrs Clinton that there are Democratic voters still unwilling to fully get on board her campaign.
With Mr Trump now the Republican presumptive nominee, it was the Democratic race that provided the focus for Tuesday’s primaries.
Mr Sanders’s victory in West Virginia, where Mrs Clinton convincingly beat Barack Obama in 2008, will prolong the Democratic contest.
In a speech delivered in Salem, Oregon, which holds its primary next week, Mr Sanders vowed to fight on.
“We have now won primaries and caucuses in 19 states and let me be as clear as I can be – we are in this campaign to win the Democratic nomination.”
He pointed to polls as evidence that he remained the best Democratic candidate to beat Donald Trump.
And he turned his fire on the billionaire property developer for insulting women, Hispanics, Muslims, African Americans and veterans.
Despite his differences with the former secretary of state Mrs Clinton, Mr Sanders said, they had one common goal – defeating Mr Trump.
Exit polls in West Virginia suggested one-third of those who voted for Mr Sanders would switch to Mr Trump in a general election between the two men.