To understand why Moodle behaves in the slightly strange way it does now, you have to understand what the code was like in Moodle 1.9, before we tried to introduce the new theme system with layout files. There was only so much we could do, because it had to be a feasible job to convert all the working Moodle 1.9 code to work with Moodle 2.0.
So, what actually happens is that scripts all over Moodle, for example mod/quiz/attempt.php, or login/index.php, or whatever, will do something like
echo $OUTPUT->header();
// ... code to output a lot of HTML goes here.
echo $OUTPUT->footer();
What happens is that $OUTPUT->header(); gets the content of the whole layout file, and returns everything before [MAIN CONTENT GOES HERE]. It stores everything after that marker, so it can later be returned by $OUTPUT->footer();
If you are lucky, the // ... code to output a lot of HTML goes here. bit has actually been re-factored, so the code looks like
$quizrenderer = $PAGE->get_renderer('mod_quiz');
echo $quizrenderer->header();
echo $quizrenderer->attempt_page();
echo $quizrenderer->footer();
and you can apply the techniques of http://docs.moodle.org/dev/Themes_2.0_overriding_a_renderer.
Depending on the type of HTML filtering you want to, you may be able to achieve what you want by creating a Filter plug-in (http://docs.moodle.org/dev/Filters). Basically, that will work if you are only interested in filtering user-input HTML.
If you want to know more about how Moodle was architected, you may find http://www.aosabook.org/en/moodle.html helpful.