"No one can stop me, if it pleases God, from being a candidate in 2024," the 48-year-old former tax inspector told a press conference.
"I can't see any judge sentencing me to five years in jail," Sonko said.
Sonko -- who came third in Senegal's presidential election in 2019 -- has enjoyed a rapid political rise and is popular with young people.
He has been charged with raping and sending death threats to an employee of a beauty salon where he went for a massage.
Sonko has denied the accusation and repeatedly said he is the victim of a "plot" by President Macky Sall.
His indictment in March 2021 triggered violence in which around a dozen people were killed.
An investigative judge on Tuesday ordered Sonko to stand trial on the charges in a criminal court.
Sonko's lawyer Cheikh Khoureyssi Ba has said he would appeal the ruling.
Sonko said it was "a real fake legal affair, a strictly political affair, orchestrated by Macky Sall's government, with the sole aim of pushing aside a candidate who appears by far one of the best placed to win the next presidential election."
Sonko's supporters hail his efforts to speak up against corruption and social elitism, as well as the economic and political grip of multinational firms and former colonial power France.
In recent years, several other prominent opponents of the president have had their political careers cut short by legal cases.
Authorities have denied any misuse of state institutions in the legal proceedings against Sonko.
Sall, who has denied any role in the Sonko affair, has won the last two presidential elections.
But he has remained vague on whether or not he intends to seek a third mandate, which would require changes to the current constitution under which a president cannot serve more than two consecutive terms.