The result was a thundering second-term victory for Mr. Modi and his party, which promised to give India a larger role on the global stage by staying tough on Pakistan and encouraging investment and development in one of the world’s fastest-growing economies.

Mr. Modi’s party won 303 seats in the Lok Sabha, the lower house of India’s Parliament, compared with 52 for Congress, which failed to reach even the minimum 10 percent of seats required to lead the opposition. The defeat was so crushing that Mr. Gandhi lost his seat in the district of Amethi, which his family had held for decades. (He won, however, in a second seat in the southern state of Kerala.)

Immediately after the votes were tallied, Mr. Gandhi, 49, offered to resign, but party leaders urged him to stay on, with crowds of supporters gathering outside of his house in New Delhi, shouting, “Rahul, don’t quit!”

In his resignation letter, Mr. Gandhi said he had ultimately stepped down because he felt responsible for the election loss, though he made it clear he would still play a role in the party. He urged restoration of India’s “once cherished institutional neutrality” and warned of “unimaginable levels of violence and pain” against minorities if the country’s democracy was not protected.

“My fight has never been a simple battle for political power,” he wrote. “I have no hatred or anger toward the B.J.P. but every living cell in my body instinctively resists their idea of India.”

For now, analysts say the future looks challenging for Congress, the party that played a leading role in India’s struggle to break away from Britain and then governed the country for most of its post-independence history.

It has been led by four generations of the Nehru-Gandhi dynasty, which takes the Gandhi name not from the independence leader but from the marriage of Nehru’s daughter Indira to another Congress activist with that surname.