He studied zoology at the University of Pavia, graduating in 1900 with a thesis on Hymenoptera found in Lombardy. In 1883 he undertook a zoological expedition to eastern Sudan and Eritrea, in which he collected a number of Hymenoptera species new to science. In 1900, he returned to Eritrea, where he spent two months collecting more zoological specimens.
He donated his African collections to the zoological museums in Genoa and Milan. In addition to his work with African species, he conducted research of Hymenoptera native to Burma. During the last years of his life, he worked as a curator at the Natural History Museum of Milan. Magretti was the author of 28 publications on Hymenoptera.
- Sugli imenotteri della Lombardia, 1881 - On Hymenoptera of Lombardy.
- Risultati di raccolte imenotterologiche nell'Africa Orientale, 1884 - Results in regards to Hymenoptera collected in East Africa.
- Nel Sudàn orientale : ricordi d'un viaggio in Africa per studii zoologici, 1884 - On eastern Sudan; reports on a journey in Africa for zoological study.
- Viaggio di L. Fea in Birmania e regioni vicine ... Imenotteri, 1892 - Travels by Leonardo Fea in Burma and neighboring regions ... Hymenoptera.
- Imenotteri. Parte prima. Mutillidei, Scoliidei, Tifiidei, Tinnidei; colla descrizione di parecchie nuove specie (with Leonardo Fea), 1892 - Hymenoptera, Part 1, Mutillidae, Scoliidae, Tiphiidae, Tinnidae; with descriptions of several new species.
- Materiali per la conoscenza della Fauna eritrea, 1905 - treatise on Eritrean fauna.