Note: much of this is source from the German Wikipedia, which doesn't have English equivalents for some of the quoted articles.
This can be traced even further back to the late 18th and throughout the 19th century, when the German nations (there wasn't a single one yet) considered itself das Land der Dichter und Denker, as literary historian Wolfgang Menzel said
Die Deutschen thun nicht viel, aber sie schreiben desto mehr. Wenn
dereist ein Bürger der kommenden Jahrhunderte auf den gegenwärtigen
Zeitpunkt der deutschen Geschichte zurückblickt, so werden ihm mehr
Bücher als Menschen vorkommen. […] Er wird sagen, wir haben geschlafen
und in Büchern geträumt. […] Das sinnige deutsche Volk liebt es zu
denken und zu dichten, und zum Schreiben hat es immer Zeit.
The German don't do a lot, but they make up for it by writing even
more. One day, when a citizen of future centuries looks back at the
current [i.e. 1828] moment of German history, they'll encounter more
books than people. [...] They will say that we slept and dreamt in
books. [...] The sensual German people loves to think and confabulate
[lit. write fiction or poetry]. It always has time to write.
It may also go back to Johann Carl August Mutäus in the 18th century
The term was also used derogatorily by people in other countries and was then warped, in light of German militarism and totalitarianism in the 20th century to Land der Richter und Henker (land of judges and executioners).
The origins go back to enlightened, though still absolutist, monarchs like Frederick the Great. But it's also linked with the development of a Bildungsbürgertum, a bourgeois elite that saw education as a high goal. In this sense it took denken out of dichten und denken, focusing on its meaning of thinking and devising things.
As @Michael Hardy wrote, this was supported by an excellent school and university system. It allowed the bourgeois to obtain high social standing through education instead of noble birth.
However, lest this be thought as some great meritocracy
Dadurch baute das Bildungsbürgertum Bildungs- und Sprachbarrieren auf,
die es zu einer elitären Schicht werden ließ, zu der Ungebildete nur
schwer Zutritt gewannen.
The educated bourgeoisie built up barriers in said education and
language which led them to become an elite into which the uneducated
could not easily gain access.
Interestingly, it also became a path to assimilation for German Jews, which is why there were so many great Jewish scientists and engineers from Germany. Until the turn towards the idea of Blood and Volk around the turn of the 19th/20th century segregated German Jews regardless of how much they supported the Fatherland.
Jews who entered the Bildungsbuergertum embraced the ideal of Bildung,
a process of intellectual expansion, aesthetic refinement, and moral
aspiration. Discord between “social-political” and
“cultural-epistemological” variants of Bildung, however, complicated
their enthusiasm, as A. Assman has shown. Ultimately, Germans armed
with Fichte and the Grimms abandoned eternal, universal Bildung for
their own particular historical Volksnation. As Volk outstripped
Bildung at the turn of the century anti-Semitic vitriol spread despite
Jewish support of the Fatherland. (source: Review by
Gregory Kaplan (Stanford Humanities Center and Department of Religious Studies, Stanford University) of German Jews: A Dual Identity by Paul Mendes-Flohr, page 1