LONDON—Prince Philip was discharged Thursday from a London hospital after he was treated for an infection, Buckingham Palace said.

Philip, the husband of Queen Elizabeth II, was admitted to the King Edward VII Hospital on Tuesday as a precautionary measure after he fell ill with an infection related to an existing health condition that wasn’t made public.

Philip, who turned 96 earlier this month, was driven from the hospital Thursday. Officials said he was in good spirits during his two nights’ stay there.

His brief admission to the hospital meant he missed out on two major events in the queen’s calendar — the state opening of Parliament on Wednesday and the Royal Ascot horse races.

In May, Philip announced he was stepping down from public life and wouldn’t take on new charity roles after more than six decades of service.

The royal has been hospitalized several times in recent years, including for treatment of a blocked coronary artery in 2011 and a bladder infection in 2012, but he has been in generally good health.

The queen, 91, maintains her regular schedule of public appearances.