Gul's withdrawal became inevitable after he failed to win enough backing from the assembly in two rounds of voting.
A new parliament will now choose a president after the July 22 general election. Parliament elects the president in Turkey for a seven-year term.
Ahmet Necdet Sezer, Turkey's current president, who had been due to retire on May 16, will stay on as interim head of state until his successor can be chosen.
Turkey's secular elite, including opposition parties, top judges and army generals, had been determined to block Gul's election.
They feared the ex-Islamist might try to undermine Turkey's separation of state and religion, a claim he and his ruling AK Party strongly deny.
Turkey's constitutional court ruled that at least 367 deputies must be in the chamber at the time of voting for the results to be valid.
In the two rounds of voting on Gul's candidacy, parliament lacked a quorum due to an opposition boycott.
Analysts say the centre-right AK Party is likely to win most votes in the coming parliamentary election but it may fail to win an outright majority, forcing it to form a coalition with one or more smaller political parties.