The idea that the Avot (forefathers) kept the Torah before it was given, has it's foundation in Bereishis 26:5, where it says:
inasmuch as Abraham obeyed Me and kept My charge: My commandments, My laws, and My teachings.”
In his commentary on this pasuk, the Rashbam explains that the words חוקותי ותורותי refer to the laws known to mankind prior to the giving of the Torah:
חוקותי ותורותי, basically these include all the laws known to mankind prior to the giving of the Torah, such as not murdering, not stealing, etc. They include virtues such as extending hospitality. At the time the Torah was revealed, these were renewed and elaborated on. At that time the covenant to keep these laws forever was concluded with the Jewish people.
The Ramban on this pasuk says that most of the mitzvot Avraham kept, applied only to the time when Avraham lived in Eretz Yisrael:
Furthermore, his observance of the Torah applied only in the Land of Israel
I found a commentary from the Chizkuni on Bereishis 2:16 where he links the word ויצו to the establishing of courts and judges in order to deal with disputes and violations of G-ds mitzvot.
ויצו, a reference to establishing courts and judges to deal with disputes and violations of G-d’s commandments. The Torah refers to this again in Genesis 18,19 when explaining why G-d discusses the imminent destruction of the Sodomites with Avraham.
There are a few interesting points that I would like to point out:
- The Ramban explains that the observance of the Torah (this is the case with Avraham) was sometimes restricted to Eretz Yisrael
- The Rashbam explains that חוקותי ותורותי refer to the universal known laws given prior to Matan Torah, these include laws such as not murdering and not stealing. Thus, the Rashbam explains that חוקותי ותורותי refer to basic moral laws.
The Jews left Mitzrayim (from Meitzar מיצר, meaning limitations but also distress as in Tehillim 118:5) and went on a "spiritual and elevating journey towards the Torah, towards a connection with G-d". Thus, after receiving the Torah, it makes sense that G-d instructs the Jewish people to establish courts. Because, on the plain level, as the Bartenura explains in his commentary on Avos 3:2 it was in order to make sure the power of the government was "divided" or limited.
The Bartenura explains in Avos 3:2
so too people, were it not for the fear of the government, each one who is bigger than his fellow would swallow his fellow (Avodah Zarah 54b).
That is the reason why G-d instructs us to establish courts and judges.
But, the mitzvah of establishing courts, also has a deeper meaning, a spiritual meaning. The Ran (Derashos HaRan 11) explains:
But they also need judges for an additional reason — to enforce the laws of the Torah and to punish those liable to stripes or to judicial death penalties, whether or not their transgression is detrimental to society.
So, it seems to me this mitzvah only applied when the Jews were living in Eretz Yisrael, as Devarim 16:18 states:
You shall appoint magistrates and officials for your tribes, in all the settlements that the L-RD your G-d is giving you
And it seems also to me that this mitzvah applied only after the giving of the Torah, since the main goal (see the commentary of the Ran) is to enforce Torah law