After briefly teaching what he had learned in Lima, he decided to return to Arequipa, only to find out that Silvia had been persuaded by her parents to be against a relationship with him.
In 1814, the revolution of Mateo Pumacahua took place in Cusco, which upset the apparent tranquillity of the Spanish viceroyalty of Peru and prompted Melgar to join the independence cause and march to combat.
The Spanish General Ramirez, after holding Melgar captive, ordered his officers to place him before a firing squad.
In a brief letter before his death, Melgar famously wrote to Spanish officials: "Cover your eyes, since you're the ones who must be disabused because America will be free in less than ten years!"
This battle – between the royalist (Spanish) and nationalist (republican) troops – sealed the independence of Peru and South America from Spain.