Opposition leader Anwar Ibrahim faces a crucial election next week, seeking a victory that could put him in striking distance of Malaysia’s premiership and help him fend off a sodomy charge that threatens his political career.

Every night, thousands of supporters turn out here among the rice fields and industrial parks of northern Malaysia as Mr. Anwar campaigns for a parliamentary seat in a by-election Tuesday. At rally after rally, Mr. Anwar stirs the crowds with vows to topple the National Front government and pursue an agenda of political and economic overhauls.

The charismatic Mr. Anwar is widely viewed as the only Malaysian politician who can unite a broad alliance of opposition parties and attract voters who are increasingly alienated from the National Front. In March, the coalition he heads won almost half the total popular vote and came within 30 seats of toppling the government in national elections.

That prospect has rattled the National Front, which has ruled Malaysia for almost 51 years, but has seen its popularity wane in recent months amid internal bickering and a slowing economy in this resource-rich Southeast Asian country of 27 million.

The by-election campaign has taken on a sharp edge because Mr. Anwar is also fighting a sodomy charge filed against him earlier this month by state prosecutors, who say he broke Malaysian law by having sex with a male aide — an allegation the 61-year-old father of six calls “preposterous” and politically motivated.

His pending prosecution makes it is possible that Mr. Anwar, who is free on bail while awaiting trial, could win next week’s election only to lose his seat if he is later convicted of sodomy.

That hasn’t stopped the National Front from deploying its biggest guns to Permatang Pauh to campaign against Mr. Anwar. Deputy Prime Minister Najib Abdul Razak, for example, has portrayed Mr. Anwar as a power hungry politician who is willing to say anything to get elected, while denying the government has anything to do with the sodomy charge against him.

Despite the National Front’s attacks, most political analysts expect Mr. Anwar to comfortably win in the predominantly Muslim Permatang Pauh constituency where he grew up and where his wife previously held the parliamentary seat that is being contested.

Mr. Anwar’s government-backed opponent, Arif Shah Omar Shah, attracts much smaller audiences at his rallies and spends much of his time in meetings with his entourage of campaign workers. He declined to be interviewed for this article.

The size of Mr. Anwar’s expected triumph will be watched closely, however; should he poll fewer votes than his wife did, the National Front would trumpet the result as a moral victory. In an interview, Mr. Anwar acknowledges he needs a convincing win “to send the message that there is a strong alternative agenda to that of the government.”

Getting elected could prove to be an easier task for Mr. Anwar than maneuvering his unwieldy opposition alliance into power — even if he manages to avoid being convicted of sodomy.

Mr. Anwar leads a diverse and sometimes fragile opposition alliance that cuts across Malaysia’s fractious multiethnic society of Malays, Chinese and Indians. It includes the left-leaning, mostly ethnic-Chinese Democratic Action Party and the Malay-centered fundamentalist Parti Islam, known as PAS, as well as Mr. Anwar’s own secularist People’s Justice Party. That is a potentially unstable mix in Malaysia, where about 60% of the population consists of Malay Muslims, who are often wary of the country’s large non-Muslim minorities.

Once a firebrand Islamic student leader, Mr. Anwar has since nurtured a reputation as a thoughtful moderate, committed to democratic principles and free-market economics. “Mr. Anwar is the glue that binds the opposition alliance together,” says Shamsul Ahmad Baruddin, an anthropology professor at the National University of Malaysia. “He’s the only one who can bring these disparate groups together in protest. And that’s why his opponents are ready to knock him down at any cost.”

Still, Mr. Anwar concedes that the opposition alliance still needs work to bind it firmly together. Of particular concern is PAS. While the conservative Islamist party’s leadership frequently repeats its support for Mr. Anwar and the opposition alliance, some PAS members worry that a potential Anwar-led government will be too secular and give too big a voice to non-Malays.

Meanwhile, Mr. Anwar’s election campaign is making the most of his image as a political martyr and would-be scourge of Malaysia’s establishment. In 1998, the politician was sacked from the government as he prepared to challenge then-Prime Minister Mahathir Mohamad for the premiership. He was later arrested, beaten by Malaysia’s national police chief and jailed for six years on sodomy and other charges in what Mr. Anwar contends was a political conspiracy to end his career. Malaysia’s highest court overturned Mr. Anwar’s sodomy conviction in 2004. Dr. Mahathir has denied there was a political conspiracy.