Police said the people of the city of Colquechaca, in southern Bolivia, seized Santos Ramos after he had been identified as the suspected rapist and murderer of Leandra Arias Janco.
Prosecutor Jose Luis Barrios said mourners tied up Santos and brought her to Janco's funeral, then threw him in her open grave and filled it with earth.
Barrios said residents blocked the roads to prevent police from interfering.
Lynchings are fairly common in Bolivia, particularly in small communities, where people mistrust corrupt law enforcement and instead police themselves.
Colquechaca is a village of about 5,000 inhabitants in the Potosi district of Bolivia.