There is no reason you couldn’t use the simple past tense in the first example.
I explained this rule to you for half a lesson before I gave a test
There is also no reason you couldn’t use the past perfect tense in the second sentence.
They had been married for a few years before divorcing in 2016.
These are both grammatical, as are your original examples. The use of before, when, and other explicit indications of relative time serve to make it not strictly necessary to use tense to indicate relative time, so the chance of ambiguity is reduced in this kind of situation. It is not coincidental that the second sentence also has such explicit indications of the relative time of each clause.
The distinction here is that my modified first example might imply that that explaining took place immediately prior to the test—that is, half a lesson was dedicated to explaining the rule in question, and then a test on that subject was given in the second half of the lesson. Adding had here will separate the two clauses more in time, leading to a stronger expectation that the half-lesson on the rule was further back in time before the test.
Let me emphasize, however, that neither sentence is entirely unambiguous about how much time did or didn’t pass between the explaining and the test-giving. If it were necessary to be very clear on that, another clause explicitly saying so would be necessary.
Anyway, that consideration doesn’t come into play for the second sentence—divorce is the end of the marriage, so the couple in question was “married” for the entire time up until the divorce in 2016. That means that the possible implication that the divorce immediately followed “being married” is entirely accurate—they really were married right up until they weren’t. Note that this means that the verb here, really, is were, while married is an adjective describing the couple’s marital status (as “Yes,” basically). We could change this to had married a few years before or had been married a few years before or had been married for a few years before, at which point that entire clause is the verb. There is nothing wrong with doing this, but it also doesn’t add any meaning, or even implicit nuance, to the sentence—which means most English speakers aren’t going to do it. It makes the verb more complicated without adding anything to the sentence.
Ultimately, what this example comes down to is the flexibility of the word married. It can be an adjective describing an ongoing marital status (“the married man”), or a verb describing taking part in a particular event at a specific point in time (the wedding, or even more precisely whatever moment during the ceremony is considered to be the point where the couple’s marital status changes, whether that’s the exchanging of vows, a kiss, signing a document, or whatever). Moreover, as a verb, it can be transitive (“he married them,” where he is the officiant and they are the now-married couple), or intransitive (“they married,” where they are the now-married couple). The flexibility on being transitive can also be used to change the voice without actually changing the meaning of a sentence in some cases (“they married” and “they were married” would ordinarily mean exactly the same thing, that they are now married).