This is the law of the peace offering (Leviticus 7:11)
Korban shelamim is often translated a “peace offering,” but that name may suggest that it is somehow meant to “appease” God for some misdeed. While that is the intent of many sacrifices, the korban shelamim is different. It’s a sacrifice brought freely to God just because a person wants to. One common reason is to thank God for some goodness He has bestowed. (This particular type of shelamim is called a korban todah, a “thanksgiving” offering.)
Aside from the korban todah, other shelamim were brought on holidays, or to fulfill oaths or vows. Shelamim could be brought from cattle, sheep or goats of either gender and most any age, but not from birds. Most kinds of shelamim also had loaves offered with them. The breast and thigh of the shelamim were given to the kohanim. (In the case of the offering brought upon completing a period as a Nazir, the kohanim also received the shoulder.)
There was only one communal free-will offering: two sheep that were offered on the holiday of Shavuos along with two loaves. While the one communal shelamim, called “shalmei tzibbur,” was of a high degree of sanctity (kadshei kadashim), individual shelamim were of a lighter degree of holiness (kadshim kalim).
As we’ve said before, the purpose of sacrifices in general is to bring us closer to God. However, we are unable to discern the underlying reason of every detail of every different type of sacrifice.
This mitzvah applies to male kohanim in Temple times. It is discussed in the Talmud in tractate Zevachim, particularly on pages 54b-55b. It is codified in the Mishneh Torah in the ninth chapter of Hilchos Maaseh HaKarbanos. It is #66 of the 248 positive mitzvos in the Rambam’s Sefer HaMitzvos.