To keep the solution as it is you can change the first for-each that it only consider the first occurrence of an id.
<xsl:for-each select="/foo/bar/baz[not (preceding::baz/@id = @id)] ">
This is by far not the best solution for this kind of "problem". To improve this have a look for " Grouping Using the Muenchian Method" (E.g.. And also it is better practice to use apply-templates instead of for-each.
Here a key based solution:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="kBazId" match="bar/baz" use="@id"/>
<xsl:template match="/" >
<xsl:for-each select="/foo/bar/baz[count( . | key('kBazId', @id)[1])=1]" >
<xsl:value-of select="@id" />
<ul>
<xsl:apply-templates select="key('kBazId', @id)/.." />
</ul>
</xsl:for-each>
</xsl:template>
<xsl:template match="bar">
<li>
<xsl:value-of select="@name"/>
</li>
</xsl:template>
</xsl:stylesheet>