Which one matters more - grades or qualifications - for admission to post-graduate study in Germany?
Since you're specifically asking about the situation in Germany, here are a few remarks that might be interesting for you (although the main point of these remarks is that it is essentially impossible to answer the question at the given level of generality).

Admission rules at German universities depend on various things: for instance, the specific university/universities you're applying to, your intended field of study, and the specific programme(s) your applying to. Also, admissions for scholarships will strongly depend on the particular scholarship(s) you're applying for.

Things might also depend a lot on whether you are a citizen of a country within the European Union. Some (but certainly not all) Master programmes in Germany have quite loose and simple rules of admission for German citizens and thus, by EU regulations, also for EU citizens. For instance, for many Master programmes in mathematics, a Bachelor's degree in mathematics with a certain grade will suffice for admission.
(But one important issue here is, of course, how grades will be converted between different academic systems.)
On the other hand, things can be more complicated (and more expensive, by the way) for international students from outside the EU.

Concerning the &quot;top university vs. mediocre university&quot; stuff: Generally speaking, prestige of universities plays a much smaller role in the German academic system than, for instance, in the US (even if an applicant has done their undergraduate studies in a country where such prestige questions play a huge role, chances are that this won't be weighted as much in the decision process at a German university as the applicant might expect).
But then again, as pointed out above, things depend a lot on the particular circumstances, and there is at least a non-zero chance that such prestige questions might play a certain role in the admission process. For some programmes that are specifically designed for international students, it can even happen that private agencies are involved in the admission process and, for instance, make a recommendation on whether an applicant should be admitted or not. Whether such agencies take the purported prestige (or ranking) of universities into account will, again, depend on various circumstances.
As a corollary of the cultural aspect described above, I should probably add the following point: In case that the application process includes the submission of a cover letter or a statement of purpose or a personal interview (which might be more likely for applications to scholarships than for applications to study programmes - but again, it probably depends), my advice would be to be a bit careful in case that you're in situation 1) described in the question. If your application leaves the impression &quot;My grades haven't been good so far, but my degree is from a top university, which certainly makes up for the bad grades&quot;, there's a considerable probability that this attitude will backfire.