Where did the mecha genre originate from in Japan? I was wondering where mecha genre in Japan originated. It doesn't seem the same as something like the idea of a doll having a soul, which is from Japanese folk beliefs, but it could be. 

Is there some historical basis for giant fighting robots (or similar things) in Japan? If not, where did they originate from, and what was the first series that showed them?
There is no deep origin of the super robot trope in Japanese folklore, at least as it pertains to anime and manga. While some Japanese legends and history (such as the samurai) may have influence the design and characteristics of the mechas, there is little direct inspirational influence.

The first anime adaptation of the Tetsujin 28-go manga, in 1963, is widely credited as the origin of the mecha genre with an early design of a "super robot". This series, in conjunction with the manga Giant Robo, were the first to feature these designs. Mitsuteru Yokoyama*, as the creator of both these manga, is considered the father of the genre.

Yokoyama was largely influenced by the wartime of his days; the bombing of Kobe is one such case. He was also influenced by the monster concept from Frankenstein. Astro Boy, a 1952 manga series, has also had great influence over the design of the robots seen in the mecha genre&#91;1&#93;.

  
(Left: Tetsujin 28-go manga cover; Right: Giant Robo manga cover)

A bit more history: Notable mecha series that followed Yokoyama's creations were Mazinger (one of the first to feature piloting) and the Gundam series. In the 90s, Neon Genesis Evangelion moved forward from the clunky, overly mechanized (and often blocky) mechas from the prior series, and introduced sleeker, faster designs&#91;2&#93;. Later series, such as Gurren Lagann and RahXephon mimicked this design, while few shows like GaoGaiGar did not.

* - Yokoyama also made significant other contributions to Japanese comic culture; these can be found in the linked WIkipedia article.