Sharapova ended the match with 51 errors to 42 winners as she pressed too hard in trying to break down Sevastova's brilliant defence.
The Latvian went close to taking the first set after coming back from 4-1 down, but Sharapova clinched it with two magnificent forehands in game 12.
The momentum had already begun to swing though, and Sevastova would level thanks to a single break at 2-1 in the second - in one rally lobbing Sharapova twice, prompting the Russian to scramble left-handed in desperation.
A six-minute bathroom break ahead of the decider apparently did not have the desired effect as Sharapova quickly fell 3-0 down.
She had said after her opening match that "this girl has a lot of grit", and it was in evidence as she cut the deficit to 3-2, but in the end Sevastova had too much.
Sharapova fought off three match points before a big first serve left the five-time major winner flailing at a return that flew wide after two hours and 17 minutes.
Analysis - a match too far for Sharapova
BBC Sport tennis correspondent Russell Fuller
Sevastova carved out a hard-earned victory with some mighty defence, and clever use of the drop shot and slice to drag Sharapova into the net.
The Russian was outstanding in patches, but lost her way completely in the deciding set. Her forehand simply disintegrated, as she hit 18 unforced errors and just four winners.
Sharapova is projected to return to the world's top 100 as a result of her run to the fourth round. She won three more matches than many thought she would after a summer disrupted by injuries, before a lack of match practice appeared to catch up with her.
And even though the US Open may not appreciate this just now, it is encouraging for the WTA Tour that a current top 20 player is able to close out victory over an opponent who may have won five Grand Slams, but has featured in only five events in the past 19 months.