The Brazilian had reported feeling dizzy after FP2 on Friday, and was taken to the medical centre and then local hospital for precautionary checks. He was then examined by the FIA medical delegate on Saturday morning, who passed him fit to compete in FP3.
During the session however Massa's symptoms returned, prompting the Brazilian to call an early halt to his running.
With regulations requiring any stand-in to take part in at least one practice session before being able to compete in a Grand Prix, Williams have elected to call on their reserve driver Di Resta to replace Massa for qualifying - which counts as a practice session - and the race at the Hungaroring.
The Scot, a veteran of 58 Grands Prix, last competed in F1 69 races ago at the 2013 Brazilian Grand Prix, finishing 11th for Force India. He has remained active since however, twinning his Williams reserve role (which he's held since 2016) with racing duties for Mercedes in the German DTM touring car series. He currently sits 12th in that championship with one victory to his name this year.
In a statement, Williams said: "After feeling unwell during practice in Budapest on Friday and Saturday, Felipe Massa will not drive for the remainder of the Hungarian Grand Prix weekend.
"Felipe visited the medical centre and the MH EK Honvedkorhaz hospital for precautionary tests on Friday, after feeling unwell and dizzy during FP2. He was cleared to take part in Saturday’s practice session by the FIA medical delegate, but he felt unwell again during FP3 and has made the decision to withdraw from the weekend.
"Williams supports Felipe’s decision and the team will work with him to ensure he makes a full recovery, with a view to return to the race track for the Belgian Grand Prix.
"Following this decision, the team’s Reserve Driver Paul di Resta will drive alongside Lance Stroll for the remainder of the Hungarian Grand Prix weekend."
Di Resta will race with Number 40 - Williams' registered reserve driver number.