The girl took the family’s AK-47 assault rifle, shot dead two of the militants and wounded several others, local officials in Ghor province said.
The Taliban came to the house because the girl’s father was a government supporter, they added.
A photo of the girl holding the gun has gone viral in recent days.
Later more militants came to attack the house, in the village of Griwa, but were beaten back by villagers and pro-government militia.
Officials said the girl, believed to be aged between 14 and 16, and her younger brother had been taken to a safer place.
Social media users praised the teenager.
“Hats off to her courage,” AFP quoted Najiba Rahmi as saying on Facebook.
“We know parents are irreplaceable, but your revenge will give you relative peace,” said Mohamed Saleh, also on Facebook.
According to local media, Ghor is one of the most underdeveloped western provinces of Afghanistan and incidences of violence against women are high.
The Taliban signed a peace deal with the US in February but many of its members continue to call for the overthrow of the current Afghan government and constitution.