Brahmananda Reddy polled 97,076 votes while Silpa Mohan Reddy secured 69,610 votes. The Congress finished far behind with just 1,382 votes.
In what was considered a prestige battle by both the parties, Brahmananda Reddy took the lead from the very first round of counting. Except in the 16th round, the TDP candidate maintained the majority till the completion of counting in 19 rounds.
About 80 per cent of the 2.16 lakh voters had cast their votes in the by-election held on August 23.
Nagi Reddy, who crossed over to the TDP last year, died in April, causing the vacancy.
TDP fielded Bhuma Nagi Reddy's nephew Brahmananda Reddy while Silpa Mohan Reddy, who switched loyalties after being denied a ticket by the TDP, contested as the YSR Congress candidate.
The by-election result is seen as a referendum on the performance of the TDP-BJP government. It is also expected to indicate the direction in which the wind may blow in the 2019 elections.
Celebrations broke out in the TDP camp in Nandyal and other parts of the state as the party workers took to the streets, dancing to drum beats, bursting firecrackers and distributing sweets.
Both the parties had campaigned extensively in the bitterly fought election. Top leaders had made personal attacks on each other during the campaign.
YSR Congress chief Y.S. Jaganmohan Reddy had sparked a row by stating during the campaign that it would not be wrong if Chief Minister N. Chandrababu Naidu is shot dead on the road or hanged for failing to fulfil his promises.
Taking exception to these remarks, the Election Commission of India sent a warning letter to Jaganmohan Reddy. On a complaint by the returning officer, a police case was also registered against him.
After the 2014 election, this is the first time that the two parties were locked in a key poll battle. Though by-elections for three assembly seats were held since 2014, they avoided a contest keeping in view the tradition to allow unanimous election for a seat falling vacant by the death of a sitting legislator.
This time, the YSR Congress rejected the TDP's appeal not to field a candidate on the ground that it had won the Nandyal seat in the 2014 elections.
The result is also a blow to the YSR Congress party's campaign to mount pressure on the TDP over the defection of its 20 legislators to the ruling party during the last two years. Alleging that the TDP lured its legislators with money, the opposition party had been challenging the TDP to make the defectors resign and face the by-elections.
Four of these MLAs were even inducted into the state cabinet in April this year, eliciting strong protests from the YSR Congress.
They include Akhila Priya, daughter of Bhuma Nagi Reddy. She represents Allagadda constituency.
Interestingly, she was elected as the YSRCP candidate in the by-election caused by the death of her mother Shoba Nagi Reddy in a road accident during the 2014 elections.