Mahler was born in Baden, Baden-Württemberg, Germany, on August 1, 1826. He was involved with the uprising of 1848–1849, and was a comrade of future Union General Carl Schurz during this period. Mahler was captured and was condemned to death for his part. However, he managed to escape. He came to the United States in 1851 at 24 years of age. He arrived in New York on the SS Charlemagne on August 20 of that year. Mahler married Jennie M. from Massachusetts.
In August 1861, following the outbreak of the American Civil War, Mahler assisted Philadelphia liquor merchant Henry Bohlen to recruit and organize a regiment. This unit would eventually be designated the 75th Pennsylvania Infantry. Mahler was named lieutenant colonel. When Colonel Henry Bohlen was promoted to brigadier general, on July 20, 1862, Mahler received his commission as colonel and took command of the 75th Pennsylvania Infantry. Mahler was wounded while leading the regiment on the second day at the Second Battle of Bull Run. At the Battle of Chancellorsville, the 75th Pennsylvania Infantry was one of the first units subjected to General "Stonewall" Jackson's flank attack, and was routed along with the rest of the 11th Corps.
During the Battle of Gettysburg, the 75th Pennsylvania Infantry was heavily engaged on the first day, in open fields north of town. The men of this unit, having advanced a considerable distance after passing through the town, held ground just east of the Carlisle Road. The fighting intensified with the collapse of other regiments forming the Union line, and the 75th Pennsylvania Infantry was exposed to a murderous fire, that rapidly depleted the ranks. Mahler was wounded in the leg at the same time that his horse was shot out from under him. He managed to extricate himself from under the animal and, although crippled, continued to direct the troops under his command. However, as the regiment began to fall back, having been outflanked by the Confederates, Mahler received a mortal gunshot wound. Lieutenant T. Albert Steiger, at considerable risk to his own life, went to Mahler's assistance. Mahler was carried from the field with General Carl Schurz clasping his hand and weeping. Mahler's brother, Lieutenant Louis Mahler, a member of the same regiment, was also killed during this same firefight. However, it is unclear whether Mahler was aware of his brother's death. Mahler died in a field hospital at Gettysburg on the morning ofJuly 4, 1863. He was 37 years old. Mahler is buried in Section B, Lot 263 of Mount Peace Cemetery in Philadelphia, Pennsylvania.